java - Parse JSON using only org.JSON -


i have been struggling accomplish thought simple org.json. receive json looks this:

{     "startdate": "2016-08-22t19:07:20.000z",     "enddate": "2016-08-23t19:07:20.000z",     "products": [{         "name": "<device level product>",         "deviceidsensoridarr": [{             "deviceid": 13124,             "sensorid": null         }],         "dataproductid": 1,         "dataproductformatid": 2,         "searchtypeid": 5,         "searchnodeid": 115,         "sitedeviceid": null,         "resourcetypeid": 1500,         "resourceid": 183,         "parameter": {             "35": "1",             "38": "1",             "40": ["41", "0"]         }     }, {         "name": "air temperature",         "deviceidsensoridarr": [{             "deviceid": 13124,             "sensorid": 9044         }],         "dataproductid": 1,         "dataproductformatid": 2,         "searchtypeid": 5,         "searchnodeid": 115,         "sitedeviceid": null,         "resourcetypeid": 1501,         "resourceid": 5235,         "parameter": {             "35": "1",             "38": "1",             "40": ["41", "0"]         }     }] } 

i need "deviceid" value each object in "products" array. retrieving entries in object such "name" or "searchnodeid" easy i'm struggling "deviceid" because second layer down. code below have far. can the first layer of values such name not deviceid. i'm sure there simpler way other libraries allowed use org.json , using first time.

        values = wp.getformparameters("parameters");         jsonobject obj = new jsonobject(values);         jsonarray products = obj.getjsonarray("products");         for(int = 0; < products.length(); ++i){             string name = products.getjsonobject(i).getstring("name");             jsonobject deviceidstr = products.getjsonobject(i).getjsonobject("deviceidsensoridarr");             string deviceid = deviceidstr.getstring("deviceid");         } 

edit

to clairfy point of question, can't figure out how use org.json functions second layer of json string. write method parse out deviceid myself there has way of doing org.json haven't yet figure out.

try this-

jsonobject obj = new jsonobject(content);     jsonarray products = obj.getjsonarray("products");     for(int = 0; < products.length(); ++i){         string name = products.getjsonobject(i).getstring("name");         jsonarray array = products.getjsonobject(i).getjsonarray("deviceidsensoridarr");         (int j = 0; j < array.length(); j++) {             string string = array.getjsonobject(j).getstring("deviceid");             system.out.println(string);         }     } 

output was

13124

13124


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -