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

时间:2017-06-28 14:18:00

标签: java json web-services rest

我正在尝试从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 个答案:

答案 0 :(得分:2)

在示例中,JSON“ASSET”包含一个集合。这可以通过"ASSET": [看出。您需要改为使用JSON Array

使用json.org库回答

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.
}

使用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.
}

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

答案 1 :(得分:1)

嗯...... JSON

这一部分的方括号
"ASSET": [ ... ]

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

将其解析为数组或更正服务端(如果它应该是对象)。

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

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

答案 2 :(得分:1)

你应该使用getJSONArray

JSONArray jsonObj4 = json.getJSONArray("ASSET");
相关问题