{ "name": "DR", "value": "", "id": "2", "open": false, "icon": null, "children": [ { "name": "1", "value": "", "id": "3", "open": false, "icon": null, "children": [ { "name": "1.1", "value": "", "id": "4", "open": false, "icon": null, "children": [ { "name": "1.1.1", "value": "", "id": "5", "open": false, "icon": null, "children": [ { "name": "检查所见", "value": "1.1.1.1内容", "id": "6", "open": false, "icon": null, "children": null, "target": "", "pId": null, "url": null, "title": null }, { "name": "诊断意见", "value": "1.1.1.2内容", "id": "7", "open": false, "icon": null, "children": null, "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null }, { "name": "2", "value": "", "id": "8", "open": false, "icon": null, "children": [ { "name": "2.1", "value": "", "id": "9", "open": false, "icon": null, "children": [ { "name": "2.1.1", "value": "", "id": "10", "open": false, "icon": null, "children": [ { "name": "检查所见", "value": "2.1.1.1内容", "id": "11", "open": false, "icon": null, "children": null, "target": "", "pId": null, "url": null, "title": null }, { "name": "诊断意见", "value": "2.1.1.2内容", "id": "12", "open": false, "icon": null, "children": null, "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } ], "target": "", "pId": null, "url": null, "title": null } 其实是一个Ztree树用到的json! 数据很多,没全部弄出来 格式大概就是这个样, 比如这段json 我要根据id取到 value, 把value值传给前台弹出显示, 比如这段json里给后台传的id为11,value就是 2.1.1.1内容 就这样
@Test public void test3() { String json = "上面的json串"; JSONObject obj = JSON.parseObject(json); Map<String, String> map = new HashMap<String, String>(); deal(obj, map); System.out.println(map); } public void deal(JSONObject obj, Map<String, String> map) { if (obj != null) { String id = obj.getString("id"); String value = obj.getString("value"); map.put(id, value); JSONArray children = obj.getJSONArray("children"); if (children != null) { for (int i = 0; i < children.size(); i++) { JSONObject child = children.getJSONObject(i); deal(child, map); } } } }alibaba 开源的fastjson 是可以的
引用来自“mklop2014”的评论
可以用Gson解析成指定对象Gson gson = new Gson(); ReportTemplateModel obj =gson.fromJson(json_, ReportTemplateModel.class); System.out.println("obj——"+obj); System.out.println("obj.getName()——"+obj.getName()); System.out.println("obj.getChildren()——"+obj.getChildren()); System.out.println("obj.getId()——"+obj.getId()); System.out.println("obj.getValue()——"+obj.getValue()); JSONArray getJsonArray=JSONArray.fromObject(obj.getChildren()); System.out.println("getJsonArray——"+getJsonArray); JSONObject getJsonObj = getJsonArray.getJSONObject(1);//获取json数组中的第一项 System.out.println("getJsonObj——"+getJsonObj); System.err.println("-------------"); ReportTemplateModel obj2 =gson.fromJson(getJsonObj.toString(), ReportTemplateModel.class); JSONArray getJsonArray2=JSONArray.fromObject(JSONArray.fromObject(obj2.getChildren())); System.out.println(getJsonArray2); JSONObject getJsonObj2 = getJsonArray2.getJSONObject(0); ReportTemplateModel obj3 =gson.fromJson(getJsonObj2.toString(), ReportTemplateModel.class); JSONArray getJsonArray3=JSONArray.fromObject(JSONArray.fromObject(obj3.getChildren())); System.out.println(getJsonArray3); JSONObject getJsonObj3 = getJsonArray3.getJSONObject(0); ReportTemplateModel obj4 =gson.fromJson(getJsonObj3.toString(), ReportTemplateModel.class); JSONArray getJsonArray4=JSONArray.fromObject(JSONArray.fromObject(obj4.getChildren())); System.out.println(getJsonArray4);引用来自“mklop2014”的评论
可以用Gson解析成指定对象引用来自“看了看”的评论
Gson gson = new Gson(); ReportTemplateModel obj =gson.fromJson(json_, ReportTemplateModel.class); System.out.println("obj——"+obj); System.out.println("obj.getName()——"+obj.getName()); System.out.println("obj.getChildren()——"+obj.getChildren()); System.out.println("obj.getId()——"+obj.getId()); System.out.println("obj.getValue()——"+obj.getValue()); JSONArray getJsonArray=JSONArray.fromObject(obj.getChildren()); System.out.println("getJsonArray——"+getJsonArray); JSONObject getJsonObj = getJsonArray.getJSONObject(1);//获取json数组中的第一项 System.out.println("getJsonObj——"+getJsonObj); System.err.println("-------------"); ReportTemplateModel obj2 =gson.fromJson(getJsonObj.toString(), ReportTemplateModel.class); JSONArray getJsonArray2=JSONArray.fromObject(JSONArray.fromObject(obj2.getChildren())); System.out.println(getJsonArray2); JSONObject getJsonObj2 = getJsonArray2.getJSONObject(0); ReportTemplateModel obj3 =gson.fromJson(getJsonObj2.toString(), ReportTemplateModel.class); JSONArray getJsonArray3=JSONArray.fromObject(JSONArray.fromObject(obj3.getChildren())); System.out.println(getJsonArray3); JSONObject getJsonObj3 = getJsonArray3.getJSONObject(0); ReportTemplateModel obj4 =gson.fromJson(getJsonObj3.toString(), ReportTemplateModel.class); JSONArray getJsonArray4=JSONArray.fromObject(JSONArray.fromObject(obj4.getChildren())); System.out.println(getJsonArray4);Gson gson = new Gson(); List<Map<String, List<Map<String, String>>>> categoryList = gson.fromJson(jsonObject.optString("msg"), new TypeToken<List<Map<String, List<Map<String, String>>>>>() {} .getType()); //处理数据如果你知道json的基本结构的话可以这样处理。不过,我格式化你的json代码后,感觉这个层次挺多的,你可以试试递归你这个问题的关键不是如何解析JSON,而是取值方式不对。
你传给后台的id是11,那就在后台只取出该条记录就可以了,为什么还要把整个树形结构的数据都返回来呢。
引用来自“拈花微笑”的评论
你这个问题的关键不是如何解析JSON,而是取值方式不对。
你传给后台的id是11,那就在后台只取出该条记录就可以了,为什么还要把整个树形结构的数据都返回来呢。
引用来自“ruislan”的评论
第一,既然你已经把包含所有层级的json数据都拿出来,何必还要去调用服务器,直接利用js就可以展开显示了。第二,如果你想要点开一层再去服务器取出来显示,那就不需要一次拿出所有的层级数据,点某层级的展开再去取这一层的子集不是这个意思额。 这个json数据是读取外部的一个json文件里面的数据 不是数据库的, 页面树已经直接通过流读取json返回给ztree生成了 已经生成树了 ,现在我点击子节点要把点击的那个子节点最里面的子节点里的数据弹出来 。。 就是那个value的数据
引用来自“拈花微笑”的评论
你这个问题的关键不是如何解析JSON,而是取值方式不对。
你传给后台的id是11,那就在后台只取出该条记录就可以了,为什么还要把整个树形结构的数据都返回来呢。
引用来自“看了看”的评论
怎么只取出该条记录的 数据 .? 要是像查数据库那样 就好了 直接sql搞定引用来自“asdf10”的评论
@Test public void test3() { String json = "上面的json串"; JSONObject obj = JSON.parseObject(json); Map<String, String> map = new HashMap<String, String>(); deal(obj, map); System.out.println(map); } public void deal(JSONObject obj, Map<String, String> map) { if (obj != null) { String id = obj.getString("id"); String value = obj.getString("value"); map.put(id, value); JSONArray children = obj.getJSONArray("children"); if (children != null) { for (int i = 0; i < children.size(); i++) { JSONObject child = children.getJSONObject(i); deal(child, map); } } } }