【问题标题】:How to Parse this JSON Response in JAVA如何在 JAVA 中解析这个 JSON 响应
【发布时间】:2013-09-24 19:21:31
【问题描述】:

我想解析这些 Json 响应:

{
"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"
    }]
 }
}

我尝试使用 SIMPLE JSON 解析器,但这对我不起作用:

Object obj = parser.parse(resp);
JSONObject jsonObject = (JSONObject) obj;
JSONArray response = (JSONArray) jsonObject.get("MyResponse");

//JSONArray arr=new JSONArray(yourJSONresponse);
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<response.size(); i++){
    list.add(response.get(i)("name"));
}

【问题讨论】:

  • 我认为JSONObject 不接受结尾的, 逗号。
  • JSON 消息不是有效的 JSON,所以这可能是您的问题。我刚刚使用 jsonlint.com 检查了您的 JSON 字符串
  • @nickebbitt 一些解析器接受,
  • 您的 json 中没有 listTsmResponse 键 ...
  • @SotiriosDelimanolis 欢呼,不知道

标签: java json


【解决方案1】:

你可以这样做:

JSONObject response = new JSONObject(resp);

然后你可以根据变量的类型使用类似的东西:

int count = response.getint("count");

JSONArray tsm = response.getJSONArray(listTsm)

然后,如果您想遍历内部的对象,只需使用 for 即可。

【讨论】:

    【解决方案2】:
    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);
        ....
    

    【讨论】:

    • 我收到类似“构造函数 JSONObject(String) 未定义”的错误
    • ... JSONObject jsonObject = new JSONObject(); JSONObject myResponse = jsonObject.getJSONObject("MyResponse"); ...
    • 你使用的是哪个版本
    • @MaximShoustin 可以直接发送链接吗?找不到相关结果。
    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多