[英]JQuery JSON getting nested value
我沒有用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);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id='result'></span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.