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.

W.K.S
  • 9,787
  • 15
  • 75
  • 122

3 Answers3

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).

PandaBearSoup
  • 699
  • 3
  • 9
  • 20
  • 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. – PandaBearSoup Jun 17 '13 at 18:59
  • 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? – W.K.S Jun 17 '13 at 19:02
  • 1
    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. – PandaBearSoup Jun 17 '13 at 19:08
  • Please provide which line is getting the exception and I can help you better. – PandaBearSoup Jun 17 '13 at 19:31
  • 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 :) – W.K.S Jun 17 '13 at 19:44
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).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
-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
}
Kishan_KP
  • 4,488
  • 6
  • 27
  • 46
  • 1
    Code without commentary is not useful. Your answer doesn't answer the question "Why am I getting this error." either. – Sumurai8 Aug 21 '13 at 09:35