我试图解码这个 json,但没有运气,这些方括号让我感到困惑任何帮助将不胜感激,这是我的 json
[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]
谢谢
试试这个:
var_export( json_decode( '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]' ) );
json_decode
返回数组或object
. 你可以用var_export
not打印它echo
您可以访问值:
$items = json_decode('[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]');
foreach( $items as $each ){
echo $each->location[0]->building[0];
echo '<hr />';
echo $each->location[0]->name;
echo '<hr />';
echo $each->name; // default organization
}
您的 json 是有效的,可能是您在访问数组内的对象时遇到问题。
print_r 永远是了解数组结构的好朋友。试试这个
$json = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]';
$decoded = json_decode($json);
echo '<pre>';
print_r($decoded);
$location = $decoded[0]->location;
$building = $location[0]->building[0];
$name = $location[0]->name;
位置 0 的对象将仅返回第一项,如果您的数组有多个值,则使用 foreach
似乎它是一个有效的 JSON。
$my_json = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]';
$my_data = json_decode($my_json);
print_r($my_data);
// 输出
Array
(
[0] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[building] => Array
(
[0] => Default Building
)
[name] => Default Location
)
)
[name] => Default Organization
)
)