HPÊý×éתJSONº¯Êýjson_encodeºÍJSONתÊý×éjson_decodeº¯ÊýµÄʹÓ÷½·¨
À´Ô´: ÍøÂç
Ëæ»úΪÄúÍƼöµÄÎÄÕÂ:AjaxʵÏÖ¶Ô¾²Ì¬Ò³ÃæµÄÎÄÕ·ÃÎÊͳ¼Æ¹¦ÄÜʾÀý
±¾ÎĽ«Îª¹Ø×¢Ö¯ÃÎÕßµÄÅóÓÑÌṩµÄÊǵÄAjaxʵÏÖ¶Ô¾²Ì¬Ò³ÃæµÄÎÄÕ·ÃÎÊͳ¼Æ¹¦ÄÜʾÀýÏà¹Ø½Ì³Ì£¬¾ßÌåʵÀý´úÂëÇë¿´ÏÂÎÄ:±¾ÎÄʵÀý½²ÊöÁËAjaxʵÏÖ¶Ô¾²Ì¬Ò³ÃæµÄÎÄÕ·ÃÎÊͳ¼Æ¹¦ÄÜ¡£·ÖÏí
ÔÚÕâÀォΪ´ó¼Ò½éÉܵÄÊÇHPÊý×éתJSONº¯Êýjson_encodeºÍJSONתÊý×éjson_decodeº¯ÊýµÄʹÓ÷½·¨µÄÏà¹Ø½éÉÜ£¬¾ßÌå´úÂëÇë¿´ÏÂÎÄ
ÕâÁ½¸öº¯Êý±È½Ï¼òµ¥£¬ÎÒÕâÀïÖ±½ÓдÀý×Ó£¬µ«ÊÇÓÐÒ»µãÒ»¶¨Òª×¢Ò⣬jsonÊý¾ÝÖ»Ö§³Öutf-8¸ñʽ£¬GBK¸ñʽµÄÊý¾Ýת»»Îªjson»á±¨´í£¡
json_encode()Ó÷¨£º
<?php$data =array(‘name’=>’jianqingwang’,‘sex’=>’man’,‘title’=>’PHPER’,‘location’=>’XiaMen’);
$new_data =json_encode($data);
var_dump($data);echo “<br><br>”;var_dump($new_data);
µÃµ½µÄÊý¾Ý£º
array(4) { [“name”]=> string(12) “jianqingwang” [“sex”]=> string(3) “man” [“title”]=> string(5) “PHPER” [“location”]=> string(6) “XiaMen” }
string(71) “{“name”:”jianqingwang”,”sex”:”man”,”title”:”PHPER”,”location”:”XiaMen”}”
json_decode()º¯ÊýÓÃÀ´°ÑjsonÊý×éת»»ÎªÊý×éµÄ£¬Ó÷¨ÈçÏ£º
jsonµÄÊý¾ÝΪ£º{“status”:1,”data”:{“prefix”:134,”province”:”±±¾©”,”city”:”±±¾©”,”isp”:”Òƶ¯”,”code”:10,”zipcode”:100000,”types”:”ÖйúÒƶ¯ GSM”,”mobile”:”13488888888″},”message”:”success”}
ÓÃjson_decodeת»»ºó£º
<?php $data =file_get_contents('http://sj.apidata.cn/?mobile=13488888888');//»ñÈ¡½Ó¿ÚµÄjsonÊý¾Ý $new_data =json_decode($data); var_dump($new_data);µÃµ½ÈçϽá¹û£º
object(stdClass)#1 (3) { [“status”]=> int(1) [“data”]=> object(stdClass)#2 (8) { [“prefix”]=> int(134) [“province”]=> string(6) “±±¾©” [“city”]=> string(6) “±±¾©” [“isp”]=> string(6) “Òƶ¯” [“code”]=> int(10) [“zipcode”]=> int(100000) [“types”]=> string(16) “ÖйúÒƶ¯ GSM” [“mobile”]=> string(11) “13488888888” } [“message”]=> string(7) “success” }
ºÜÏÔÈ»£¬ÎÒÃÇת»»jsonÊý¾ÝºóµÃµ½Ò»¸ö¶ÔÏó£¬ÎÒÃÇÒªÔõôÓÃËüÄØ£¿
Ö±½ÓÓÃ
$new_data->data->isp//Òƶ¯ $new_data->data->province//Ê¡·ÝÕâÑùµÄÐÎʽ·ÃÎÊ£¬ÎªÊ²Ã´ÕâÑù·ÃÎÊ£¬ÎÒÃÇ¿´ÏÂËüµÄÊý¾Ý½á¹¹¾ÍÖªµÀÁË
²©¿ÍÔÎÄ£ºÊý×éתJSONº¯Êýjson_encodeºÍJSONתÊý×éjson_decodeº¯ÊýµÄʹÓ÷½·¨Í¨±¾Ñ§Ï°ÄúÊDz»ÊǸüÁ˽âphpÁËÄØ£®¸Ðл¹Ø×¢Ö¯ÃÎÕß
ÕâЩÄÚÈÝ¿ÉÄܶÔÄãÒ²ÓаïÖú¸ü¶àPHP½Ì³Ì¿É²é¿´PHP½Ì³ÌÁбíÒ³¡£
TAGS: Êý×é º¯Êý HP json JSON
¡¡