Ä¿Ç°£¬JSONÒѾ³ÉΪ×iÁ÷ÐеÄÊý¾Ý½»»»¸ñʽ֮һ£¬¸÷´oÍøÕ¾µÄAPI¼¸ºo¶¼Ö§³ÖËu¡£
ÎÒд¹ýһƪ¡¶Êý¾ÝÀaÐͺÍJSON¸ñʽ¡£¬Ì½ÌÖËuµÄÉe¼Æ˼Ïe¡£½ñÌi£¬ÎÒÏe×ܽaÒ»ÏÂPHPÓiÑÔ¶ÔËuµÄÖ§³Ö£¬ÕaÊÇ¿ª¢»¥ÁªÍøÓ¦ÓóÌÐo£¨ÌرðÊDZaдAPI£(C)±ØÐeÁ˽aµÄ֪ʶ¡£
´Ó5.2°ae±¾¿ªÊ¼£¬PHPÔÉuÌa¹(C)json_encode()ºÍjson_decode()º¯Êý£¬Ç°ÕßÓÃÓÚ±aÂe£¬ºoÕßÓÃÓÚ½aÂe¡£
Ò»¡¢json_encode()
¸Ãº¯ÊýÖ÷ÒªÓÃÀ´½«Êý×eºÍ¶ÔÏo£¬×ª»»Îªjson¸ñʽ¡£ÏÈ¿´Ò»¸oÊý×eת»»µÄÀý×Ó£º
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
½a¹uΪ
{"a":1,"b":2,"c":3,"d":4,"e":5}
ÔÙ¿´Ò»¸o¶ÔÏoת»»µÄÀý×Ó£º
$obj->body = 'another post';
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj);
½a¹uΪ
{
¡¡¡¡¡¡¡¡"body":"another post",¡¡
¡¡¡¡¡¡¡¡"id":21,
¡¡¡¡¡¡¡¡"approved":true,
¡¡¡¡¡¡¡¡"favorite_count":1,
¡¡¡¡¡¡¡¡"status":null
}
ÓÉÓÚjsonÖ»½ÓÊÜutf-8±aÂeµÄ×Öu£¬ËuÒÔjson_encode()µÄ²ÎÊý±ØÐeÊÇutf-8±aÂe£¬ñÔo»aµÃµ½¿Õ×Ö*u»oÕßnull¡£µ±ÖÐÎÄʹÓÃGB2312±aÂe£¬»oÕßÍaÎÄʹÓÃISO-8859-1±aÂeµÄʱºo£¬ÕaÒ»µaÒªÌرð×¢Òa¡£
¶þ¡¢Ë÷ÒýÊý×eºÍ¹ØÁªÊý×e
PHPÖ§³ÖÁ½ÖÖÊý×e£¬Ò»ÖÖÊÇÖ»±£´ae"Öµ"£¨value£(C)µÄË÷ÒýÊý×e£¨indexed array£(C)£¬ÁiÒ»ÖÖÊDZ£´ae"ÃuÖµ¶Ô"£¨name/value£(C)µÄ¹ØÁªÊý×e£¨associative array£(C)¡£
ÓÉÓÚjavascript²»Ö§³Ö¹ØÁªÊý×e£¬ËuÒÔjson_encode()Ö»½«Ë÷ÒýÊý×e£¨indexed array£(C)תΪÊý×e¸ñʽ£¬¶ø½«¹ØÁªÊý×e£¨associative array£(C)תΪ¶ÔÏo¸ñʽ¡£
±ÈÈ磬ÏÖÔÚÓÐÒ»¸oË÷ÒýÊý×e
$arr = Array('one', 'two', 'three');
echo json_encode($arr);
½a¹uΪ£º
["one","two","three"]
Èç¹u½«Ëu¸ÄΪ¹ØÁªÊý×e£º
$arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
echo json_encode($arr);
½a¹u¾Í±aÁË£º
{"1":"one","2":"two","3":"three"}
×¢Òa£¬Êý¾Ý¸ñʽ´Ó"[]"£¨Êý×e£(C)±a³ÉÁË"{}"£¨¶ÔÏo£(C)¡£
Èç¹uÄaÐeÒª½«"Ë÷ÒýÊý×e"Ç¿ÖÆת»¯³É"¶ÔÏo"£¬¿ÉÒÔÕaÑuд
json_encode( (object)$arr );
»oÕß
json_encode ( $arr, JSON_FORCE_OBJECT );
Èý¡¢Àa£¨class£(C)µÄת»»
ÏÂÃaeÊÇÒ»¸oPHPµÄÀa£º
class Foo {
¡¡¡¡¡¡¡¡const ERROR_CODE = '404';
¡¡¡¡¡¡¡¡public $public_ex = 'this is public';
¡¡¡¡¡¡¡¡private $private_ex = 'this is private!';
¡¡¡¡¡¡¡¡protected $protected_ex = 'this should be protected';
¡¡¡¡¡¡¡¡public function getErrorCode() {
¡¡¡¡¡¡¡¡¡¡¡¡return self::ERROR_CODE;
¡¡¡¡¡¡¡¡}
}
ÏÖÔÚ£¬¶ÔÕa¸oÀaµÄʵÀý½øÐÐjsonת»»£º
$foo = new Foo;
$foo_json = json_encode($foo);
echo $foo_json;
Êa³o½a¹uÊÇ
{"public_ex":"this is public"}
¿ÉÒÔ¿´µ½£¬³ýÁ˹«¿ª±aÁ¿£¨public£(C)£¬ÆaËu¶«Î÷£¨³£Á¿¡¢Ë½ÓбaÁ¿¡¢½¨µÈµÈ£(C)¶¼ÒÅʧÁË¡£
ËÄ¡¢json_decode()
¸Ãº¯ÊýÓÃÓÚ½«jsonÎı¾×ª»»ÎªÏaÓ¦µÄPHPÊý¾Ý½a¹¹¡£ÏÂÃaeÊÇÒ»¸oÀý×Ó£º
$json = '{"foo": 12345}';¡¡¡¡
$obj = json_decode($json);¡¡
print $obj->{'foo'}; // 12345
ͨ³£Çe¿oÏ£¬json_decode()×ÜÊÇ*µ»ØÒ»¸oPHP¶ÔÏo£¬¶ø²»ÊÇÊý×e¡£±ÈÈ磺
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';¡¡¡¡
var_dump(json_decode($json));
½a¹u¾ÍÊÇÉu³ÉÒ»¸oPHP¶ÔÏo£º
object(stdClass)#1 (5) {
¡¡¡¡
¡¡¡¡¡¡¡¡["a"] => int(1)
¡¡¡¡¡¡¡¡["b"] => int(2)
¡¡¡¡¡¡¡¡["c"] => int(3)
¡¡¡¡¡¡¡¡["d"] => int(4)
¡¡¡¡¡¡¡¡["e"] => int(5)
¡¡¡¡
}
Èç¹uÏeҪǿÖÆÉu³ÉPHP¹ØÁªÊý×e£¬json_decode()ÐeÒª¼ÓÒ»¸o²ÎÊýtrue£º
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json,true));
½a¹u¾ÍÉu³ÉÁËÒ»¸o¹ØÁªÊý×e£º
array(5) {
¡¡¡¡
¡¡¡¡ ¡¡¡¡["a"] => int(1)
¡¡¡¡ ¡¡¡¡["b"] => int(2)
¡¡¡¡ ¡¡¡¡["c"] => int(3)
¡¡¡¡ ¡¡¡¡["d"] => int(4)
¡¡¡¡ ¡¡¡¡["e"] => int(5)
¡¡¡¡
}¡¡
Îa¡¢json_decode()µÄ³£¼u´iÎo
ÏÂÃaeÈýÖÖjsonд*¨¶¼ÊÇ´iµÄ£¬ÄaÄÜ¿´³o´iÔÚÄÄÀiÂð£¿
$bad_json = "{ 'bar': 'baz' }";
$bad_json = '{ bar: "baz" }';¡¡¡¡
$bad_json = '{ "bar": "baz", }';
¶ÔÕaÈý¸o×Öu´®Ö´ÐÐjson_decode()¶¼½«µ»Ønull£¬²¢ÇÒ±¨´i¡£
µÚÒ»¸oµÄ´iÎoÊÇ£¬jsonµÄÖ¸ou£¨delimiter£(C)Ö»ÔÊÐiʹÓÃË«ÒýºÅ£¬²»ÄÜʹÓõ¥ÒýºÅ¡£µÚ¶þ¸oµÄ´iÎoÊÇ£¬jsonÃuÖµ¶ÔµÄ"Ãu"£¨Ã°ºÅ×o±ßµÄ²¿*Ö£(C)£¬ÈκÎÇe¿o϶¼±ØÐeʹÓÃË«ÒýºÅ¡£µÚÈý¸oµÄ´iÎoÊÇ£¬×iºoÒ»¸oÖµÖ®ºo²»ÄÜÌi¼Ó¶ººÅ£¨trailing comma£(C)¡£
ÁiÍa£¬jsonÖ»ÄÜÓÃÀ´±iʾ¶ÔÏo£¨object£(C)ºÍÊý×e£¨array£(C)£¬Èç¹u¶ÔÒ»¸o×Öu´®»oÊýֵʹÓÃjson_decode()£¬½«»aµ»Ønull¡£
var_dump(json_decode("Hello World")); //null
ÏÂÃae¸ø´o¼Ò½eÉÜŶphpÓiÑÔµÄjsonʵÏÖ
ÓÉÓÚ¿ª¢Ò»¸oajax file manager for web¿ªÔ´ÏiÄ¿,Êý¾Ý½»»»Ê¹ÓõÄjson¸ñʽ£¬ºoÀ´¢ÏÖÔڵͰae±¾µÄphpÉÏÔËÐлaÓÐÎÊÌa£¬×Ðϸµ÷ÊÔ¢ÏÖjson_decodeºÍjson_encodeÎÞ¨Õý³£¹¤×÷£¬ÓÚÊDzeÔÄ×ÊÁÏ£¬*¢ÏֵͰae±¾µÄphpûÓÐʵÏÖÕaÁ½¸oº¯Êý£¬ÎªÁ˼aeÈÝÐÔ£¬ÎÒÖ»ºÃ×Ô¼ºÊµÏÖÒ»¸ophp°aeµÄjson±aÂe½aÂe´uÂe£¬²¢±£Ö¤ºÍjson2.jsµÄÒ»Ö£¬²aÊÔµ÷ÊÔ²¢Í¨¹ý£¬ÏÖÔÚ½«Æa¹«²¼³oÀ´£¬¹(C)ÓÐÏaͬÐeÇoµÄͬѧʹÓãº
<?php
/* * ****************************************************************************
* $base: $
*
* $Author: $
* Berlin Qin
*
* $History: base.js $
* Berlin Qin // created
*
* $contacted
* webfmt@gmail.com
* www.webfmt.com
*
* *************************************************************************** */
/* ===========================================================================
* license
*
* ¡¢Open Source Licenses
* webfmt is distributed under the GPL, LGPL and MPL open source licenses.
* This triple copyleft licensing model avoids incompatibility with other open source licenses.
* These Open Source licenses are specially indicated for:
* Integrating webfmt into Open Source software;
* Personal and educational use of webfmt;
* Integrating webfmt in commercial software,
* taking care of satisfying the Open Source licenses terms,
* while not able or interested on supporting webfmt and its development.
*
* ¡¢Commercial License ¨C fbis source Closed Distribution License - CDL
* For many companies and products, Open Source licenses are not an option.
* This is why the fbis source Closed Distribution License (CDL) has been introduced.
* It is a non-copyleft license which gives companies complete freedom
* when integrating webfmt into their products and web sites.
* This license offers a very flexible way to integrate webfmt in your commercial application.
* These are the main advantages it offers over an Open Source license:
* Modifications and enhancements doesn't need to be released under an Open Source license;
* There is no need to distribute any Open Source license terms alongside with your product
* and no reference to it have to be done;
* No references to webfmt have to be done in any file distributed with your product;
* The source code of webfmt doesn't have to be distributed alongside with your product;
* You can remove any file from webfmt when integrating it with your product.
* The CDL is a lifetime license valid for all releases of webfmt published during
* and before the year following its purchase.
* It's valid for webfmt releases also. It includes year of personal e-mail support.
*
* ************************************************************************************************************************************************* */
function jsonDecode($json)
{
$result = array();
try
{
if (PHP_VERSION_ID > )
{
$result = (array) json_decode($json);
}
else
{
$json = str_replace(array("\\\\", "\\\""), array("&#