我有一個很大的文件,其中包含許多與以下類似的JSON對象。我需要解析所有的東西,使用org.json library作爲數組來獲得「buying_together」項。我無法訪問嵌套在「相關」中的任何內容。使用org.json解析Java中的JSON
將「buying_together」作爲列表檢索所需的代碼是什麼?
{
"asin": "11158732",
"title": "Girls Ballet Tutu Zebra Hot Pink",
"price": 3.17,
"imUrl": "http://ecx.images-amazon.com/images/I/51fAmVkTbyL._SY300_.jpg",
"related":
{
"also_bought": ["L00JHONN1S", "B002BZX8Z6"],
"also_viewed": ["F002BZX8Z6", "B00JHONN1S", "B008F0SU0Y", "B00D23MC6W", "B00AFDOPDA"],
"bought_together": ["D202BZX8Z6"]
},
"salesRank": {"Toys & Games": 211836},
"brand": "Coxlures",
"categories": [["Sports & Outdoors", "Other Sports", "Dance"]]
}
這是我嘗試(請注意,這是一個MapReduce的程序,所以一些線似乎斷章取義之內。):
JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line
JSONArray boughtTogether = new JSONArray(object.getJSONArray("bought_together"));
請寫任何示例代碼,如果你寫了! – iNan