0

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

Here is My output to your code

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • I would suggest you to use GSON library for JSON parsing – Ritesh Gune Aug 17 '13 at 19:00
  • What will that do differently? And will it work on android? – Zapnologica Aug 17 '13 at 19:02
  • First try to understand [Difference between JsonObject and JsonArray](http://stackoverflow.com/a/12289961/1554935) – Ritesh Gune Aug 17 '13 at 19:27
  • @RiteshGune, I know the difference between the two. I just noticed now that the server returned an object in this case, all my other cases it returned an array. But my issue is now splitting my object into an array by "children" but it is giving me an error and i cant understand why, I have checked the spelling etc. – Zapnologica Aug 17 '13 at 19:30
  • Zapnologica,no offence but just wanted to know whether you know that or not. – Ritesh Gune Aug 17 '13 at 19:34
  • Oh, No offence taken :) Sorry if i came across harsh. I just wanted to find out why its giving me these errors. I'm starting to think it might be some type of character encoding issue or some thing. I dono – Zapnologica Aug 17 '13 at 19:37

2 Answers2

3

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
    }
]
MrLore
  • 3,759
  • 2
  • 28
  • 36
  • OK i Changed it to: `JSONObject dds = (JSONObject) jsonObj.get("dds");` and that worked but now `JSONArray jsonArr = dds.getJSONArray("children");` is giving an error> Saying no value for children – Zapnologica Aug 17 '13 at 19:16
  • If by some way you are trying to get these children they are null indeed {"type":2,"order":2,"id":4,**"children":[]**}]} try putting some elements in the bold array. – qualebs Aug 17 '13 at 19:33
  • OK if i run that it also works, but i see you added `\` in there. So I am thinking it might be something wrong with my variable string result. – Zapnologica Aug 17 '13 at 19:43
  • @MrLove , Why when it prints children and grandchildren does it not display id: order: ? only children? – Zapnologica Aug 17 '13 at 19:52
  • @Zapnologica The results are alphabetised, they're underneath the children arrays but definitely there. (I've edited in the output I'm seeing). – MrLore Aug 17 '13 at 19:55
  • @Zapnologica I dunno about that, it's weird. Whatever you're running this in looks complicated though, can you run this in a simpler environment, like command prompt or an IDE while you're testing? – MrLore Aug 17 '13 at 20:11
  • Just running it on android. in eclipse – Zapnologica Aug 17 '13 at 20:12
0

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;
}
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72