2

I have the following JSON:

{
    data: [
    {
        objectType: "ServiceForbiddenException",
        item: {
            service: "users",
            action: "index",
            code: 403,
            message: "Access to the service [users] is forbidden."
        }
    }
    ]
}

I tried parsing it using the following snippet:

       String bodyData = iRes.body().string();
       try{
            JSONObject body = new JSONObject(bodyData);
            JSONArray data = body.getJSONArray("data");
            JSONObject type =  data.getJSONObject(0);
            JSONArray item = data.getJSONArray(1);

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

the problem is that is that JSONArray data = body.getJSONArray("data");is actually the whole data, and not an array.

How do I parse it correctly? are there better libs / ways to parse a json in Java (Android)

8
  • This isn't valid JSON. You could look in to gson for a JSON parser. Commented Jul 8, 2015 at 22:46
  • Looks valid to me. He has a parsing error though. Commented Jul 8, 2015 at 22:47
  • @karaokyo thanks for assisting. I tested it in Link and came out valid Commented Jul 8, 2015 at 22:48
  • @GabeSechan, where is the error? how should I parse it? thank you Commented Jul 8, 2015 at 22:49
  • 2
    To make it proper JSON you would need to surround keys with ". Commented Jul 8, 2015 at 22:53

2 Answers 2

1

Objects are created using {}, arrays are created using []. So

data:[
 { }
]

is array labelled as data which contains only one object (unlabelled, but indexed as 0), so calling

data.getJSONArray(1);

doesn't make sense here, because it would try to find

data:[
 { }
 [ ] <-this, but you don't have it in your JSON
]

What you seem to want is getting item object which is part of object you already got from array and stored in type.

So instead of

JSONArray item = data.getJSONArray(1);

use

JSONObject item = type.getJSONObject("item");
^^^^^^^^^^        ^^^^    ^^^^^^^^^^ - things you need to change
Sign up to request clarification or add additional context in comments.

1 Comment

Actually there is not many things to read. I think json.org covers all important details about JSON structure.
1

Data is an array of objects (with only 1 object in the example). So your type should be type = data.getJSONObject(0).getString("objectType"); and item = type = data.getJSONObject(0).getJSONObject("item"); Notice item is a JSONObject, not a JSONArray.

If you need to process all the items in data, loop over them and change the index you're getting from 0.

Actually looking at your code- you seem to be a little confused on Objects and Arrays in general. You might want to read up on them, with respect to JSON.

1 Comment

You are quite right here :) now, I am confused. Can you point out what I am missing? or recommend any good article? Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.