android - Trouble parsing JSON to string in Java -


i have following json:

{     data: [     {         objecttype: "serviceforbiddenexception",         item: {             service: "users",             action: "index",             code: 403,             message: "access service [users] forbidden."         }     }     ] } 

i tried parsing using 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 jsonarray data = body.getjsonarray("data");is whole data, , not array.

how parse correctly? there better libs / ways parse json in java (android)

objects created using {}, arrays created using [].

data:[  { } ] 

is array labelled data contains 1 object (unlabelled, indexed 0), calling

data.getjsonarray(1); 

doesn't make sense here, because try find

data:[  { }  [ ] <-this, don't have in json ] 

what seem want getting item object part of object got array , stored in type.

so instead of

jsonarray item = data.getjsonarray(1); 

use

jsonobject item = type.getjsonobject("item"); ^^^^^^^^^^        ^^^^    ^^^^^^^^^^ - things need change 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -