Home >Backend Development >PHP Tutorial >php parsing json error

php parsing json error

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-04 09:19:091220browse

<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?

Reply content:

<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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn