我没有在JavaScript中处理过很多JSON字符串,尽管我的研究无法解决这个简单的问题.这是我的问题:
我的JSON result
:
[{ "Id": "7884", "name": "Some Name", "location": { "distance": 3.2988, "geoCode": { "latitude": "Y", "longitude": "X" }, "address": { "street": "14706 E Example Ave.", "state": "CA", "city": "Hollywood", "country": "USA", "postalCode": "99999" } } }]
现在我解析并迭代:
var obj = JSON.parse(result); alert(obj[0].Id);
这适用于字符串的更高级别部分.
但是我不知道如何进入字符串的"地址"部分.我试过obj[0].address.street
,obj[0].address[street]
甚至obj[0].address[0]
无济于事.
有人可以指导我以正确的方式深入挖掘street
水平吗?
你应该通过location
对象,因为address
它在里面:
obj[0].location.address.street
希望这可以帮助.
var obj =[{
"Id": "7884",
"name": "Some Name",
"location": {
"distance": 3.2988,
"geoCode": {
"latitude": "Y",
"longitude": "X"
},
"address": {
"street": "14706 E Example Ave.",
"state": "CA",
"city": "Hollywood",
"country": "USA",
"postalCode": "99999"
}
}
}];
$('#result').text(obj[0].location.address.street);