简体   繁体   中英

Trouble with Parsing JSONObject in java

I am having trouble parsing this particular JSONObject,

Here is the object:

{"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":[]}]}]}}

That object is stored in my variable called result, here is my code to parse it:

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

} catch (JSONException e) { 
e.printStackTrace();
}

And it is giving me this error:

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

I am trying to break it up into sub arrays of children. Where am I going wrong?

@Mr Love

here is my output to your code

这是我对您的代码的输出

You are calling jsonArr = jsonObj.getJSONArray("dds"); , however dds is not an array, it's a JSON object, if you format the JSON you can see it clearly:

{
   "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":[

                  ]
               }
            ]
         }
      ]
   }
}

So you will just need to call JSONObject dds = jsonObj.getJSONObject("dds") , and if you want the children you would call JSONArray children = jsonObj.getJSONObject("dds").getJSONArray("children"); .

private static final String 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\":[]}]}]}}";

public static void main(String[] args) throws JSONException
{
    JSONObject jsonObj = new JSONObject(json);
    JSONObject dds = jsonObj.getJSONObject("dds");

    JSONArray children = dds.getJSONArray("children");
    System.out.println("Children:");
    System.out.println(children.toString(4));

    JSONArray grandChildren = children.getJSONObject(0).getJSONArray("children");
    System.out.println("Grandchildren:");
    System.out.println(grandChildren.toString(4));
}

Produces:

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

You can do it like this, where the JsonElement could be a JSONobject or JsonArray or any primitive type:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM