首页 文章

Java和REST Web服务 - GET方法返回JSONObject [“...”]不是JSONObject

提问于
浏览
0

我试图从REST Web服务获得JAVA响应 .

使用HTTP请求工具正确返回JSON结构 . 这是JSON响应,可以在浏览器的附加工具中看到:

{
    "QueryMXASSETResponse": {
        "rsStart": 0,
        "rsCount": 1,
        "MXASSETSet": {
            "ASSET": [
                {
                    "Attributes": {
                        "ASSETID": {
                            "content": 123
                        },
                        "ASSETNUM": {
                            "content": "SM-A-3002"
                        },
                        "DESCRIPTION": {
                            "content": "restint"
                        }
                    }

                }
            ]
        }
    }
}

这是我在Java App中的MAIN方法中的代码(我想得到DESCRIPTION值)

String response = httpGet("http://192.168.150.18:9080/maxrest/rest/os/mxasset/?assetid=~eq~123");
    System.out.println(response+"\n");             
    //Parsing JSON response
    JSONObject jsonObj = new JSONObject(response);
    if (jsonObj.has("QueryMXASSETResponse")){
        JSONObject jsonObj2 = jsonObj.getJSONObject("QueryMXASSETResponse");
        JSONObject jsonObj3 = jsonObj2.getJSONObject("MXASSETSet");
        JSONObject jsonObj4 = jsonObj3.getJSONObject("ASSET");
        JSONObject jsonObj5 = jsonObj4.getJSONObject("Attributes");         
        JSONObject jsonObj6= jsonObj5.getJSONObject("DESCRIPTION");
        System.out.println("Description is: "+jsonObj6.getString("content"));

    }

返回的错误是针对jsonObj4,它表示它不是JSONObject,尽管你可以在上面的响应中看到它是 . 为什么我得到异常?你能帮忙吗?谢谢

Exception in thread "main" org.json.JSONException: JSONObject["ASSET"] is not a JSONObject.
    at org.json.JSONObject.getJSONObject(JSONObject.java:557)
    at com.getAsset.GETAssets.main(GETAssets.java:91)

3 回答

  • 2

    你应该使用getJSONArray

    JSONArray jsonObj4 = json.getJSONArray("ASSET");
    
  • 1

    嗯... JSON的这一部分的方括号

    "ASSET": [ ... ]
    

    告诉我们 "ASSET"JSONArray 而不是 JSONObject .

    如果它应该是一个对象,则将其解析为数组或更正服务端 .

    如果JSON已修复(并且保证在资产数组中包含一个对象),那么您只需将代码更改为

    JSONObject jsonObj4 = jsonObj3.getJSONArray("ASSET").getJSONObject(0);
    JSONObject jsonObj5 = jsonObj4.getJSONObject("Attributes");         
    JSONObject jsonObj6= jsonObj5.getJSONObject("DESCRIPTION");
    
  • 1

    在示例中,JSON "ASSET"包含一个集合 . 这可以通过 "ASSET": [ 看到 . 您需要使用JSON Array代替 .

    Answer using json.org library

    JSONObject descriptionJson = null;
    if (jsonObj3.has("ASSET")) {
        JSONArray jsonArray1 = jsonObj3.getJSONArray("ASSET");
        if (jsonArray1.length() > 0) {
            JSONObject asset = jsonArray1.getJSONObject(0);
            JSONObject attributesObj = asset.getJSONObject("ATTRIBUTE");
            descriptionJson = attributesObj.getJSONObject("DESCRIPTION");
        }
    }
    if (descriptionJson != null) {
        //Do your processing here.
    }
    

    Answer using Java JSON API

    JSONObject descriptionJson = null;
    JSONArray jsonArray1 = jsonObj3.getJSONArray("ASSET");
    if (jsonArray1 != null && !jsonArray1.isEmpty()) {
        JSONObject asset = jsonArray1.getJSONObject(0);
        JSONObject attributesObj = asset.getJSONObject("ATTRIBUTE");
        descriptionJson = attributesObj.getJSONObject("DESCRIPTION");
    }
    if (descriptionJson != null) {
        //Do your processing here.
    }
    

    这假设没有任何东西可以为null,但列表可能为空 . 如果不是这样,那么您可以添加空检查或删除 isEmpty 检查 .

相关问题