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

标签 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 应用程序中的 MAIN 方法中的代码(我想获取描述值)

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)?你能帮忙吗?谢谢

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)

最佳答案

在示例中,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.
}

这是假设没有任何内容可以为空,但列表可能为空。如果情况并非如此,那么您可以添加 null 检查或删除 isEmpty 检查。

关于Java 和 REST Web 服务 - GET 方法返回 JSONObject ["....."] 不是 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805129/

相关文章:

java - 专用 Android Activity 为应用程序初始化数据和库

java - 在构建路径中包含其他项目时出现 NoClassDefFounderror

java - 每天上午 09.30 开始的 Cron 表达式,间隔 30 分钟

php - 创建一个表单以使用 MySQL 更新 JSON 数据

json - 编码(marshal)返回我的结构的空 json

json - 反序列化可以在相同属性名称下具有不同对象的 JSON

java - 在 BlackBerry 中调用 Web 服务调用时,应用程序挂起

java - Samsung S4 OS 4.3 上的时间选择器对话框崩溃

php - 在 PHP/MYSQL 中的 XML WebService 中传递参数

web-services - 安全网络服务的负载测试