json取数据怎么取?求json根据id取数据的java代码

看了看 发布于 2015/06/26 09:11
阅读 7K+
收藏 0
{ "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内容 就这样
加载中
0
asdf10
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);
                }
            }
        }
    }



0
itkang
itkang

alibaba 开源的fastjson 是可以的

看了看
有类似的代码例子么
itkang
itkang
回复 @看了看 : 如果你传递过来的是标准的json格式,通过fastJson 是可以当成key-value取值的,就跟你用map是一样的
看了看
像数据库那样 根据id查询 value 这种 先解析在根据id查 有类似的代码例子么 ?
0
itkang
itkang
JSONObject msg = JSONObject.parseObject(message.toString())
 String cmid = (String) msg.get("chainMasterId")
String type = (String) msg.get("type")
String[] ids = msg.get("ids").toString().split(";");



看了看
我这是json里嵌套数组 反正各种嵌套的 要先一层层解析才行 好纠结额
0
mklop2014
mklop2014
可以用Gson解析成指定对象
0
看了看

引用来自“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);
 
这样一层一层解 好像行不通啊   然后怎么根据id取value?


0
mklop2014
mklop2014

引用来自“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);
 
这样一层一层解 好像行不通啊   然后怎么根据id取value?


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代码后,感觉这个层次挺多的,你可以试试递归


0
SF不会玩
SF不会玩
JSONArray jary = new JSONArray(result);
JSONObject jo = null;
for (int i = 0; i < jary.length(); i++) {
jo = jary.getJSONObject(i);
Device device = new Device();
device.setDeviceid(jo.getInt("deviceid"));
device.setDevicename(jo.getString("devicename"));
device.setDeviceurl(jo.getString("deviceurl"));
device.setLocationindex(jo.getInt("locationindex"));
device.setLocationname(jo.getString("locationname"));

}

按我这个改

0
看了看

引用来自“SF不会玩”的评论

JSONArray jary = new JSONArray(result);
JSONObject jo = null;
for (int i = 0; i < jary.length(); i++) {
jo = jary.getJSONObject(i);
Device device = new Device();
device.setDeviceid(jo.getInt("deviceid"));
device.setDevicename(jo.getString("devicename"));
device.setDeviceurl(jo.getString("deviceurl"));
device.setLocationindex(jo.getInt("locationindex"));
device.setLocationname(jo.getString("locationname"));

}

按我这个改

还是行不通啊 这个问题弄了好久,你有时间能不能帮我调调 qq1026067792   灰常感谢
0
看了看

引用来自“SF不会玩”的评论

JSONArray jary = new JSONArray(result);
JSONObject jo = null;
for (int i = 0; i < jary.length(); i++) {
jo = jary.getJSONObject(i);
Device device = new Device();
device.setDeviceid(jo.getInt("deviceid"));
device.setDevicename(jo.getString("devicename"));
device.setDeviceurl(jo.getString("deviceurl"));
device.setLocationindex(jo.getInt("locationindex"));
device.setLocationname(jo.getString("locationname"));

}

按我这个改

引用来自“看了看”的评论

还是行不通啊 这个问题弄了好久,你有时间能不能帮我调调 qq1026067792   灰常感谢
而且直接JSONArray jary = new JSONArray(json);是不行的  我那个数据里面本身就嵌套了[] 数组 ..
0
DW_GYT
DW_GYT

你这个问题的关键不是如何解析JSON,而是取值方式不对。

你传给后台的id是11,那就在后台只取出该条记录就可以了,为什么还要把整个树形结构的数据都返回来呢。

返回顶部
顶部