Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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中解析JSONObject时遇到问题_Java_Json_Jsonobject - Fatal编程技术网

在java中解析JSONObject时遇到问题

在java中解析JSONObject时遇到问题,java,json,jsonobject,Java,Json,Jsonobject,我在解析这个特定的JSONObject时遇到问题 以下是对象: {"status":1,"dds":{"id":1,"name":"DDS1","description":"This is DDS 1","children":[{"id":2,"order":1,"type":1,"children":[{"id":3,"order":1,"type":3,"children":[]},{"id":4,"order":2,"type":2,"children":[]}]}]}} 该对象存储在名

我在解析这个特定的JSONObject时遇到问题

以下是对象:

{"status":1,"dds":{"id":1,"name":"DDS1","description":"This is DDS 1","children":[{"id":2,"order":1,"type":1,"children":[{"id":3,"order":1,"type":3,"children":[]},{"id":4,"order":2,"type":2,"children":[]}]}]}}
该对象存储在名为result的变量中,下面是解析该对象的代码:

JSONObject jsonObj = null;
JSONArray jsonArr = null;
try {
    jsonObj = new JSONObject(result);
    jsonArr = jsonObj.getJSONArray("dds");

} catch (JSONException e) { 
e.printStackTrace();
}
这给了我一个错误:

org.json.JSONException: Value {"id":1,"children":[{"type":1,"order":1,"id":2,"children":[{"type":3,"order":1,"id":3,"children":[]},{"type":2,"order":2,"id":4,"children":[]}]}],"description":"This is DDS 1","name":"DDS1"} at dds of type org.json.JSONObject cannot be converted to JSONArray
我试图把它分解成子数组。我哪里做错了

@洛夫先生

这是我对代码的输出


您正在调用
jsonArr=jsonObj.getJSONArray(“dds”)
,但是
dds
不是数组,它是JSON对象,如果格式化JSON,您可以清楚地看到它:

{
   "status":1,
   "dds":{
      "id":1,
      "name":"DDS1",
      "description":"This is DDS 1",
      "children":[
         {
            "id":2,
            "order":1,
            "type":1,
            "children":[
               {
                  "id":3,
                  "order":1,
                  "type":3,
                  "children":[

                  ]
               },
               {
                  "id":4,
                  "order":2,
                  "type":2,
                  "children":[

                  ]
               }
            ]
         }
      ]
   }
}
因此,您只需要调用
JSONObject dds=jsonObj.getJSONObject(“dds”)
,如果需要子对象,您可以调用
JSONArray children=jsonObj.getJSONObject(“dds”).getJSONArray(“children”)

产生:

Children:
[{
    "children": [
        {
            "children": [],
            "id": 3,
            "order": 1,
            "type": 3
        },
        {
            "children": [],
            "id": 4,
            "order": 2,
            "type": 2
        }
    ],
    "id": 2,
    "order": 1,
    "type": 1
}]
Grandchildren:
[
    {
        "children": [],
        "id": 3,
        "order": 1,
        "type": 3
    },
    {
        "children": [],
        "id": 4,
        "order": 2,
        "type": 2
    }
]

您正在调用
jsonArr=jsonObj.getJSONArray(“dds”)
,但是
dds
不是数组,它是JSON对象,如果格式化JSON,您可以清楚地看到它:

{
   "status":1,
   "dds":{
      "id":1,
      "name":"DDS1",
      "description":"This is DDS 1",
      "children":[
         {
            "id":2,
            "order":1,
            "type":1,
            "children":[
               {
                  "id":3,
                  "order":1,
                  "type":3,
                  "children":[

                  ]
               },
               {
                  "id":4,
                  "order":2,
                  "type":2,
                  "children":[

                  ]
               }
            ]
         }
      ]
   }
}
因此,您只需要调用
JSONObject dds=jsonObj.getJSONObject(“dds”)
,如果需要子对象,您可以调用
JSONArray children=jsonObj.getJSONObject(“dds”).getJSONArray(“children”)

产生:

Children:
[{
    "children": [
        {
            "children": [],
            "id": 3,
            "order": 1,
            "type": 3
        },
        {
            "children": [],
            "id": 4,
            "order": 2,
            "type": 2
        }
    ],
    "id": 2,
    "order": 1,
    "type": 1
}]
Grandchildren:
[
    {
        "children": [],
        "id": 3,
        "order": 1,
        "type": 3
    },
    {
        "children": [],
        "id": 4,
        "order": 2,
        "type": 2
    }
]

您可以这样做,其中JsonElement可以是JSONobject或JsonArray或任何基元类型:

private JsonElement findElementsChildren(JsonElement element, String id) {
    if(element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        if(id.equals(jsonObject.get("id").getAsString())) {
            return jsonObject.get("children");
        } else {
            return findElementsChildren(element.get("children").getAsJsonArray(), id);
        }
    } else if(element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        for (JsonElement childElement : jsonArray) {
            JsonElement result = findElementsChildren(childElement, id);
            if(result != null) {
                return result;
            }
        }
    }

    return null;
}

您可以这样做,其中JsonElement可以是JSONobject或JsonArray或任何基元类型:

private JsonElement findElementsChildren(JsonElement element, String id) {
    if(element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        if(id.equals(jsonObject.get("id").getAsString())) {
            return jsonObject.get("children");
        } else {
            return findElementsChildren(element.get("children").getAsJsonArray(), id);
        }
    } else if(element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        for (JsonElement childElement : jsonArray) {
            JsonElement result = findElementsChildren(childElement, id);
            if(result != null) {
                return result;
            }
        }
    }

    return null;
}


我建议您使用GSON库进行JSON解析,这会有什么不同?它能在android上工作吗?首先试着理解@RiteshGune,我知道两者之间的区别。我刚刚注意到服务器在本例中返回了一个对象,在其他所有情况下,它都返回了一个数组。但我现在的问题是通过“children”将我的对象拆分为一个数组,但这给了我一个错误,我不明白为什么,我已经检查了拼写等。Zapnologica,无意冒犯,只是想知道您是否知道。我建议您使用GSON库进行JSON解析,这会有什么不同?它能在android上工作吗?首先试着理解@RiteshGune,我知道两者之间的区别。我刚刚注意到服务器在本例中返回了一个对象,在其他所有情况下,它都返回了一个数组。但我的问题是现在将我的对象按“children”拆分成数组,但这给了我一个错误,我不明白为什么,我已经检查了拼写等。Zapnologica,无意冒犯,只是想知道你是否知道。好的,我将它改为:
JSONObject dds=(JSONObject)JSONObject.get(“dds”)
这很有效,但现在
JSONArray jsonArr=dds.getJSONArray(“儿童”)给出了一个错误>对children说没有值如果通过某种方式你试图得到这些子元素,它们确实是空的{“type”:2,“order”:2,“id”:4,“children”:[]}}尝试在粗体数组中放入一些元素。好的,如果我运行它,它也可以工作,但是我看到你在其中添加了`。所以我想我的变量字符串结果可能有问题。@MrLove,为什么它在打印子对象和孙子对象时不显示id:order:?“独生子女?”Zapnologica结果按字母顺序排列,它们在儿童数组下面,但肯定在那里。(我在看到的输出中进行了编辑)。好的,我将其更改为:
JSONObject dds=(JSONObject)jsonObj.get(“dds”)
这很有效,但现在
JSONArray jsonArr=dds.getJSONArray(“儿童”)给出了一个错误>对children说没有值如果通过某种方式你试图得到这些子元素,它们确实是空的{“type”:2,“order”:2,“id”:4,“children”:[]}}尝试在粗体数组中放入一些元素。好的,如果我运行它,它也可以工作,但是我看到你在其中添加了`。所以我想我的变量字符串结果可能有问题。@MrLove,为什么它在打印子对象和孙子对象时不显示id:order:?“独生子女?”Zapnologica结果按字母顺序排列,它们在儿童数组下面,但肯定在那里。(我在看到的输出中进行了编辑)。现在,它要求我在第一次返回中“向元素添加强制转换”,并在
下面加下划线。在redOk中,get
现在它要求我在第一次返回中“向元素添加强制转换”,并在
下面加红色下划线。get