使用JSON Java检索嵌套数组值

使用JSON Java检索嵌套数组值,第1张

使用JSON Java检索嵌套数组

使用的库:

import org.json.simple.JSONArray;import org.json.simple.JSONObject;

检查以下代码并逐步解析

    JSonObject search = (JSONObject) jsonObject.get("search");//1    JSonArray entry = (JSONArray) search.get("entry");//2    for (int i = 0; i < entry.size(); i++) {        JSonObject jsonObject1 = (JSONObject) entry.get(i);//3        JSonArray jsonarray1 = (JSONArray) jsonObject1.get("attribute");//4        for (int j = 0; j < jsonarray1.size(); j++) { System.out.println(((JSONObject) jsonarray1.get(j)).get(         "value").toString());//5        }    }

它将逐步给出所提到的值:

1){“ entry”:[{“ found”:“ identity = 9454532,l = big,ton = grand,k = molvi”,“
attribute”:[{“ name”:“ firstname”,“ value”:[ “ Lucas”]},{“ name”:“姓氏”,“
value”:[“ Brandon”]}}}}],“返回”:{“ pre”:0,“ count”:1,“ message”: “成功”}}

2)[{“ found”:“ identity = 9454532,l = big,ton = grand,k = molvi”,“
attribute”:[{“ name”:“ firstname”,“ value”:[“ Lucas”]} ,{“ name”:“
lastname”,“ value”:[“ Brandon”]}]}]]

3){“ found”:“ identity = 9454532,l = big,ton = grand,k = molvi”,“
attribute”:[{“ name”:“ firstname”,“ value”:[“ Lucas”]}, {“ name”:“
lastname”,“ value”:[“ Brandon”]}]}

4)[{“ name”:“ firstname”,“ value”:[“ Lucas”]},{“ name”:“ lastname”,“
value”:[“ Brandon”]}]

5)[“ Lucas”]和[“ Brandon”]

因此,基本上,您必须分别照顾JSONObject和JSONArray并进行相应的解析。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4900859.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存