我有一个很大的文件,其中包含许多与以下类似的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