php解析json数组问题。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php解析json数组问题。相关的知识,希望对你有一定的参考价值。

从json格式数据
"status":0,"total":1,"size":1,"contents":["title":"111","location":[113.11509,23.012777],"city":"\u4f5b\u5c71\u5e02","create_time":1385386545,"geotable_id":38432,"address":"\u5e7f\u4e1c\u7701\u4f5b\u5c71\u5e02\u7985\u57ce\u533a\u6c7e\u6c5f\u5357\u8def8\u53f7","province":"\u5e7f\u4e1c\u7701","district":"\u7985\u57ce\u533a","map_id":"1","uid":42504634,"coord_type":3,"type":0,"distance":370,"weight":0]

中,捉取title的值定义$title location经纬度定义 $location_X $location_Y distance的值定义$distance
php菜鸟在线等

$a = json_decode('"status":0,"total":1,"size":1,"contents":["title":"111","location":[113.11509,23.012777],"city":"\u4f5b\u5c71\u5e02","create_time":1385386545,"geotable_id":38432,"address":"\u5e7f\u4e1c\u7701\u4f5b\u5c71\u5e02\u7985\u57ce\u533a\u6c7e\u6c5f\u5357\u8def8\u53f7","province":"\u5e7f\u4e1c\u7701","district":"\u7985\u57ce\u533a","map_id":"1","uid":42504634,"coord_type":3,"type":0,"distance":370,"weight":0]',true);

json_decode第二个参数true

解析出来就是array(4) ["status"]=> int(0) ["total"]=> int(1) ["size"]=> int(1) ["contents"]=> array(1) [0]=> array(14) ["title"]=> string(3) "111" ["location"]=> array(2) [0]=> float(113.11509) [1]=> float(23.012777) ["city"]=> string(9) "佛山市" ["create_time"]=> int(1385386545) ["geotable_id"]=> int(38432) ["address"]=> string(43) "广东省佛山市禅城区汾江南路8号" ["province"]=> string(9) "广东省" ["district"]=> string(9) "禅城区" ["map_id"]=> string(1) "1" ["uid"]=> int(42504634) ["coord_type"]=> int(3) ["type"]=> int(0) ["distance"]=> int(370) ["weight"]=> int(0)

这样的数组会取内容了吧
参考技术A <?php
$a = json_decode('"status":0,"total":1,"size":1,"contents":["title":"111","location":[113.11509,23.012777],"city":"\\u4f5b\\u5c71\\u5e02","create_time":1385386545,"geotable_id":38432,"address":"\\u5e7f\\u4e1c\\u7701\\u4f5b\\u5c71\\u5e02\\u7985\\u57ce\\u533a\\u6c7e\\u6c5f\\u5357\\u8def8\\u53f7","province":"\\u5e7f\\u4e1c\\u7701","district":"\\u7985\\u57ce\\u533a","map_id":"1","uid":42504634,"coord_type":3,"type":0,"distance":370,"weight":0]');

$b = $a->contents;

echo $b[0]->title;
echo '<br>';
echo $b[0]->location[0];
echo '<br>';
echo $b[0]->location[1];
?>

哎,你这个对象略复杂,我的眼睛都看花了。。。

还有,城市名并没有加密,只是楼上没有指定编码utf-8

城市名是:佛山市

地址是:广东省佛山市禅城区汾江南路8号

省份是:广东省

district是:禅城区

本回答被提问者采纳
参考技术B json_decode($json);解析一下,不过里面city字段是加密过的,要看是什么方式加密的追问

不需要读取city字段,求详细示例~

以上是关于php解析json数组问题。的主要内容,如果未能解决你的问题,请参考以下文章

php解析json数组

如何在PHP中解析json

用 PHP 解析 JSON

如何从android中的php关联数组解析JSON响应?

使用 PHP foreach 解析 JSON 数组

php数组转换成json格式后汉字怎么就被解析成字母了啊