2015-09-26 21 views
-1

我是JSON的新手,並試圖理解如何遍歷從Javascript中的查詢獲得的結構。我想獲得全面的價值。用數值解析JSON對象,類型爲

我可以設法查詢query.pages,但不知道當我必須導航「4187783」時該怎麼辦。

我一直在尋找解決方案一段時間。感謝幫助!

{ 
"batchcomplete":"", 
"query":{ 
    "pages":{ 
    "418783":{ 
    "pageid":418783, 
    "ns":0, 
    "title":"San Francisco LGBT Community Center", 
    "contentmodel":"wikitext", 
    "pagelanguage":"en", 
    "touched":"2015-09-21T09:58:52Z", 
    "lastrevid":675179151, 
    "length":2449, 
    "fullurl":"https://en.wikipedia.org/wiki/San_Francisco_LGBT_Community_Center" 
    } 
    } 
} 
} 

回答

0

我能設法得到query.pages但不當我必須瀏覽「4187783」時,確定該怎麼做。

query.pages["4187783"]

對於fullurl: query.pages["4187783"].fullurl

+1

嗨克里斯,這個數字實際上是一個數字,所以你需要刪除引號。 – Saar

+0

@Saar同意,儘管一些瀏覽器支持兩種方式,因爲密鑰投射 – Chris

2

首先將您的JSON字符串對象:

var result = JSON.parse(yourJson); 

現在你可以做到以下幾點:

var fullUrl = result.query.pages[418783].fullurl; 
// you can replace 418783 with "length" or "contentmodel" to get other properties.