1

I have an Array of JSON Objects like this:

{"message":"[\"{\\\"name\\\":\\\"lays\\\",\\\"calories\\\":1.0}\",\"{\\\"name\\\":\\\"lays\\\",\\\"calories\\\":0.33248466}\"]"}

I am trying to parse it using this code:

Object object = parser.parse ( message );
JSONArray array = (JSONArray) object;
for(int i=0;i < array.size () ; i++)
{
    System.out.println(array.get ( i ));//output; {"name":"lays","calories":1.0}
    JSONObject jsonObj = ( JSONObject ) array.get ( i );//ClassCastExceptio
    String foodName = ( String ) jsonObj.get ( KEY_NAME );
    Float calories = (Float) jsonObj.get ( KEY_CARLORIES );
    Nutrinfo info = new Nutrinfo(foodName,calories);
    data.add ( info );

}

but I get a ClassCastException on the marked line. This doesn't make sense: array.get() returns an object, which I cast to a JSONObject. Why am I getting this error.

Thanks.

3 Answers 3

2

You have multiple json objects within the "message" json object. Use this code to retreve the first values (need a loop for all of them). It seems like you may want to rearrange how you are creating your json objects.

Object object = parser.parse ( message );
JSONArray array = (JSONArray) object;
for(int i=0;i < array.size () ; i++)
{
JSONArray jsonArray = ( JSONArray ) array.get ( i );
JSONObject jsonObj = (JSONObject) jsonArray.get(0);
String foodName = jsonObj.getString ( KEY_NAME );
Float calories = jsonObj.getFloat ( KEY_CARLORIES );
Nutrinfo info = new Nutrinfo(foodName,calories);
data.add ( info );
}

Remember when using JSON, each {} set of brackets signifies an individual JSON object. When dealing with multiple objects on the same level, you must first get the JSON Array (like you did for each message).

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I was using the wrong name for the second JSONArray, try now. Also, JSON has built in get methods for the primitive types, no need to cast.
I realised that, but it still doesn't work. I'm really confused about what's happening. There is only one message object in the response, which contains an array of name and calories. I parsed the message into a JSONArray object, isn't that correct?
You can not have an array inside a JSON objects without treating them as JSON arrays. You have multiple message, each contained within {} brackets, to access one of these you have to iterate through the JSON Array of these objects. Inside your Message object you have again, multiple json objects, each containing keys for calorie/etc. You must treat the Message "object" as another json array.
Please provide which line is getting the exception and I can help you better.
I got it to work finally but after removing JSONArray jsonArray = ( JSONArray ) array.get ( i ); and changing JSONObject jsonObj = (JSONObject) jsonArray.get(0); to JSONObject jsonObj = (JSONObject) jsonArray.get(i);. Thanks man :)
0

You have json content embedded within json content. the "outer" object has a key "message" with a single string value. that string value happens to be serialized json, but the json parser isn't going to handle that for you automatically. you will have to get the message value and parse that a second time. (actually, it's worse than that, you have at least 2 levels of embedded json content).

Comments

-1
Object object = parser.parse ( message );

if( object instanceof JSONObject ){
    // TODO : process with single JSON Object
}
else if( object instanceof JSONArray ){
    // TODO : process with JSONArray Object
}

1 Comment

Code without commentary is not useful. Your answer doesn't answer the question "Why am I getting this error." either.

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.