如何在JAVA中解析此JSON响应

如何在JAVA中解析此JSON响应,第1张

如何在JAVA中解析此JSON响应
public static void main(String[] args) throws JSonException {    String jsonString  = "{" +  "   "MyResponse": {" +  "       "count": 3," +  "       "listTsm": [{" +  ""id": "b90c6218-73c8-30bd-b532-5ccf435da766"," +  ""simpleid": 1," +  ""name": "vignesh1"" +  "       }," +  "       {" +  ""id": "b90c6218-73c8-30bd-b532-5ccf435da766"," +  ""simpleid": 2," +  ""name": "vignesh2"" +  "       }," +  "       {" +  ""id": "b90c6218-73c8-30bd-b532-5ccf435da766"," +  ""simpleid": 3," +  ""name": "vignesh3"" +  "       }]" +  "   }" +  "}";    JSonObject jsonObject = new JSonObject(jsonString);    JSonObject myResponse = jsonObject.getJSonObject("MyResponse");    JSonArray tsmresponse = (JSONArray) myResponse.get("listTsm");    ArrayList<String> list = new ArrayList<String>();    for(int i=0; i<tsmresponse.length(); i++){        list.add(tsmresponse.getJSonObject(i).getString("name"));    }    System.out.println(list);}   }

输出

[vignesh1, vignesh2, vignesh3]

评论: 我没有添加验证

[编辑]

加载json字符串的其他方法

    JSonObject obj= new JSonObject();    JSonObject jsonObject = obj.fromObject(jsonString);    ....


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存