我的JSON(已解码)在subdivisions
部分后面有额外的方括号,因此我无法像使用$resultArray['country']['geoname_id'];
那样定位其他部分一样。为什么还有一对额方括号?
string(1461) "{"country":{"iso_code":"CA","names":{"pt-BR":"Canadá","es":"Canadá","ru":"Канада","en":"Canada","zh-CN":"加拿大","fr":"Canada","de":"Kanada","ja":"カナダ"},"confidence":99,"geoname_id":6251999},"location":{"longitude":-79.4886,"latitude":43.7496,"time_zone":"America/Toronto","accuracy_radius":10},"subdivisions":[{"iso_code":"ON","names":{"en":"Ontario","zh-CN":"安大略","pt-BR":"Ontário","ja":"オンタリオ州","ru":"Онтарио"},"confidence":93,"geoname_id":6093943}],"postal":{"confidence":17,"code":"M3J"},.....
我指的是
"subdivisions":[{"iso_code":"ON"
这部分和结束部分。为什么细分是唯一具有方括号的东西?
答案 0 :(得分:1)
细分它是一个数组,在JSON中,这些是对象:
{key:value}
这些是数组:
[{key1:value1},{key2:value2}]
就像javascript一样。所以在这种情况下,细分可以有多个值,如果你在PHP中使用json_decode()解码它,你将得到一个包含一个对象的数组。
答案 1 :(得分:1)
如果对关联数组使用$data = json_decode($string, true);
//设置为true,则得到:
array (
'country' =>
array (
'iso_code' => 'CA',
'names' =>
array (
'pt-BR' => 'Canadá',
'es' => 'Canadá',
'ru' => 'Канада',
'en' => 'Canada',
'zh-CN' => '加拿大',
'fr' => 'Canada',
'de' => 'Kanada',
'ja' => 'カナダ',
),
'confidence' => 99,
'geoname_id' => 6251999,
),
'location' =>
array (
'longitude' => -79.4886000000000052523319027386605739593505859375,
'latitude' => 43.7496000000000009322320693172514438629150390625,
'time_zone' => 'America/Toronto',
'accuracy_radius' => 10,
),
'subdivisions' =>
array (
0 =>
array (
'iso_code' => 'ON',
'names' =>
array (
'en' => 'Ontario',
'zh-CN' => '安大略',
'pt-BR' => 'Ontário',
'ja' => 'オンタリオ州',
'ru' => 'Онтарио',
),
'confidence' => 93,
'geoname_id' => 6093943,
),
),
'postal' =>
array (
'confidence' => 17,
'code' => 'M3J',
),
)
访问数据:
$data['subdivisions'][0]['iso_code'];
$data['subdivisions'][0]['names']['en'];
您可能需要分析json以查看在执行您正在执行的操作之前是否在细分中获得了多个值