<code><?php $json = '{"detail":[{"name":24,"country":China,"location":{"city":chengdu},"code":2000}'; $obj = json_decode($json,true); echo $obj->detail[0]->name; echo $obj->detail[0]->location->city; ?> </code>
The code is as above, how should I modify it?
<code><?php $json = '{"detail":[{"name":24,"country":China,"location":{"city":chengdu},"code":2000}'; $obj = json_decode($json,true); echo $obj->detail[0]->name; echo $obj->detail[0]->location->city; ?> </code>
The code is as above, how should I modify it?
Your JSON format is not correct. Once the json string is written, you can copy it to http://www.bejson.com/ and check it
You$obj = json_decode($json,true);
You can directly echo $obj[0]['name']
afterwards. After using json_decode($json, true)
, it will be converted into an array instead of an object. Arrays can be manipulated
$obj = json_decode($json,true); What is exposed in this way is an array. Of course it is wrong if you use the object method to retrieve it
$obj = json_decode($json); What is parsed in this way is an object
Don’t construct json data manually. Be sure to use PHP function to generate json json_encode(). I checked your json. It’s not standard json data at all. A syntax error is reported