【发布时间】:2014-01-23 15:44:27
【问题描述】:
我对解析 JSON 非常陌生。我已经看了一遍,似乎无法理解我的特定问题的想法。我很难理解如何从 JSON 数组中获取 JSON 对象。我的例子如下
[{"styleId":94,
"status":"verified",
"abv":"4.2",
"name":"Bud Light"}]
这是我当前的代码
JSONParser parser = new JSONParser();
Object obj = parser.parse(inputLine);
JSONObject jsonObject = (JSONObject) obj;
Long currPage = (Long)jsonObject.get("currentPage");
System.out.println(currPage);
JSONArray jArray = (JSONArray)jsonObject.get("data");
System.out.println(jArray);
inputLine 是我的原始 JSON。我从具有“数据”标签的原始 JSONObject 中提取了一个 JSONArray。现在这就是我被卡住并在顶部给出 JSONArray 的地方。不知道如何遍历数组以获取“名称”标签。
感谢您的帮助!
【问题讨论】:
-
JSONObject obj2=(JSONObject)jArray.get(0);然后 System.out.println(obj2.get("name"));
-
为什么不使用 Jackson 之类的工具为您做 JSON 管道?
标签: java json simplejson