3

我试图解码这个 json,但没有运气,这些方括号让我感到困惑任何帮助将不胜感激,这是我的 json

[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]

谢谢

4

3 回答 3

4

试试这个:

var_export( json_decode( '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]' )  );

json_decode返回数组或object. 你可以用var_exportnot打印它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
}
于 2013-03-21T07:13:34.557 回答
1

您的 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

于 2013-03-21T07:18:38.997 回答
0

似乎它是一个有效的 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
        )

)
于 2013-03-21T07:14:42.647 回答