Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java和REST web服务-GET方法返回JSONObject[";…]不是JSONObject_Java_Json_Web Services_Rest - Fatal编程技术网

Java和REST web服务-GET方法返回JSONObject[";…]不是JSONObject

Java和REST web服务-GET方法返回JSONObject[";…]不是JSONObject,java,json,web-services,rest,Java,Json,Web Services,Rest,我正在尝试从RESTWeb服务获取JAVA响应 使用HTTP请求工具正确返回JSON结构。 这是可以在浏览器的附加工具中看到的JSON响应: { "QueryMXASSETResponse": { "rsStart": 0, "rsCount": 1, "MXASSETSet": { "ASSET": [ { "Attributes": {

我正在尝试从RESTWeb服务获取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方法中的代码(我想得到描述值)

返回的错误是针对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)

嗯。。。这部分JSON中的方括号

"ASSET": [ ... ]
告诉我们
“资产”
JSONArray
而不是
JSONObject

将其解析为数组,如果服务端本应是对象,则更正服务端

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

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

嗯。。。这部分JSON中的方括号

"ASSET": [ ... ]
告诉我们
“资产”
JSONArray
而不是
JSONObject

将其解析为数组,如果服务端本应是对象,则更正服务端

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

JSONObject jsonObj4 = jsonObj3.getJSONArray("ASSET").getJSONObject(0);
JSONObject jsonObj5 = jsonObj4.getJSONObject("Attributes");         
JSONObject jsonObj6= jsonObj5.getJSONObject("DESCRIPTION");
在示例中,JSON“资产”包含一个集合。这可以通过
“资产”:[
看到。您需要改用

使用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,但列表可能为空。如果这不是真的,则可以添加null检查或删除
isEmpty
检查。

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

使用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,但列表可能为空。如果这不是真的,则可以添加null检查或删除
isEmpty
检查。

您应该使用getJSONArray

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

您应该使用getJSONArray

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


资产不是一个JSONObject,它是一个JSONArrayHi Daniel…你能更新我的代码以便我能得到想要的结果吗?想从资产对象的描述字段中检索值..ThanksASSET不是一个JSONObject,它是一个JSONArrayHi Daniel…你能更新我的代码以便我能得到想要的结果吗?想从描述字段中检索值吗从资产对象..谢谢我没有使用过JSON数组。你能更新代码以便我能从描述列中检索值吗?谢谢你我没有使用过JSON数组。你能更新代码以便我能从描述列中检索值吗?谢谢你。然后如何从描述元素中检索值?Thank youHi。然后如何从描述元素中检索值?谢谢。Daniel。然后如何从资产/属性部分下面的描述元素中检索值?谢谢。Daniel。它不应该是空的吗?为什么是空的?这是错误的吗?我是基于Java EE的,可以被视为列表。如果您正在使用不同的JSON框架,您的方法可能会有所不同。@Dejan我现在明白您的意思了。您是对的。我已将我的答案编辑为
!…isEmpty()
Hi Daniel。我将此标记为answer。但我的对象或数组没有要检查的isEmpty方法。出现Complile错误。我是否可以以某种方式检查对象是否存在或其中是否包含以下对象?我无法找到方法isEmpty.Hi.Daniel的符号。然后如何从描述元素中检索值ch在资产/属性部分的下方?谢谢你,嗨,丹尼尔。它不应该是空的吗?为什么是空的?这是错误的吗?我是基于Java EE的,它可以被视为一个列表。如果你使用不同的JSON框架,你的方法可能会不同。@Dejan我现在明白你的意思了。你是对的。我将我的答案编辑为
!…isEmpty()
Hi Daniel。我将此标记为答案。但我的对象或数组没有要检查的isEmpty方法。出现Complile错误。我是否可以以某种方式检查对象是否存在或其中是否包含以下对象?我无法找到方法isEmpty的符号。