【发布时间】:2016-11-06 22:32:35
【问题描述】:
我有一个包含许多类似于以下内容的 JSON 对象的大文件。我需要使用org.json library 解析所有内容以将“bought_together”项目作为数组获取。我无法访问嵌套在“相关”中的任何内容。
检索“bought_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"));
【问题讨论】:
-
如果您写过任何示例代码,请发布!