java - JSONArray cannot be converted to JSONObject exception -


i have json string structured in following way , throws exception passing jsonarray timejsonarray = new jsonarray(time);

this error value [{"daysbyte":158,"from":1020,"to":1260},{"daysbyte":96,"from":1020,"to":1320}] @ 0 of type org.json.jsonarray cannot converted jsonobject how receive array , can't change it, i'm having trouble converting json object instead of json string format it's in. doing wrong?

[       [         {            "daysbyte":30,          "from":660,          "to":1290       },       {            "daysbyte":96,          "from":660,          "to":1320       },       {            "daysbyte":128,          "from":1050,          "to":1290       }    ],    [         {            "daysbyte":252,          "from":690,          "to":840       },       {            "daysbyte":252,          "from":1050,          "to":1260       }    ] ] 

this code working with. i'm getting value passed in string

public arraylist<string> gettimelist(string time){          system.out.println("places activity " + time);         arraylist<string> times = new arraylist<>();         try{             //jsonobject timejsonobject = new jsonobject(time);             jsonarray timejsonarray = new jsonarray(time);             arraylist<legacytimespan> timespanlist = new arraylist<>();              legacytimespanconverterimpl converter = new legacytimespanconverterimpl();               for(int = 0; < timejsonarray.length(); i++){                  int daysbyte = timejsonarray.getjsonobject(i).getint("daysbyte");                 int = timejsonarray.getjsonobject(i).getint("from");                 int = timejsonarray.getjsonobject(i).getint("to");                  system.out.println("to " + to);                  legacytimespan timespan = new legacytimespan(daysbyte, from, to);                 timespanlist.add(timespan);              }              log.d("time span list", timespanlist.tostring());             weekspan weekspan = converter.converttoweekspan(timespanlist);             list<daytimespanpair> daytimespanpair = weekspan.todaytimespanpairs();             for(int = 0; i< daytimespanpair.size(); i++){                  string timerange = buildtimestring(daytimespanpair.get(i));                 times.add(timerange);               }         } catch(jsonexception e){             log.d("places exception json",e.getmessage());         }          return times;     } 

this code should work think u declare json format.

             [                 [                   {                   } ,{},{}             // json object structure u defined in question toparray =      ],                 [                     {                   },{},{}                 ]              ]   for(jsonarray objarray : toparray){     for(jsonobject eachobject : objarray){    system.out.println(eachobject.get("daysbyte"););    system.out.println(eachobject.get("from");    system.out.println(eachobject.get("to");     } } 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -