How to process Json array in android without square bracket -


raw json data

 [      {         "worker.1":{            "last_share":1443639029,          "score":"3722204.62578",          "alive":true,          "shares":1124332,          "hashrate":1047253       },       "worker.2":{            "last_share":1443639029,          "score":"3794755.69049",          "alive":true,          "shares":1069332,          "hashrate":1012070       },       "worker.3":{            "last_share":1440778690,          "score":"0.0",          "alive":false,          "shares":0,          "hashrate":0       },       "worker.4":{            "last_share":1443638222,          "score":"940190.67723",          "alive":true,          "shares":449932,          "hashrate":404772       }       "worker.nth":{            "last_share":1443638222,          "score":"940190.67723",          "alive":true,          "shares":449932,          "hashrate":404772       }    } ] 

from main question have been able narrow down results , managed add square bracket well. have json array above advise me how can process json array. note can end being 1000 workers.

please please have been breaking head 2 days on this, note actual json data complex , 1000's of entries, json data included overview.

edit: code ma using process above json

try{                         jsonarray jarray = new jsonarray(jsonobj.getjsonobject("workers"));                         if(jarray.length() == 0){                             log.d("json err", "json string has no entries");                             return null;                         }                         jsonobject jobject = jarray.getjsonobject(0);                          // 1 specific element:                         jsonobject worker1 = jobject.getjsonobject("worker.1");                         jsonobject worker2 = jobject.getjsonobject("worker.2");                          // iterate on elements                         iterator<?> keys = jobject.keys();                         while( keys.hasnext() ) {                             string key = (string)keys.next(); // "worker.1", etc                             if ( jobject.get(key) instanceof jsonobject ) {                                 jsonobject entry = jobject.getjsonobject(key);                                 // entry here like:                                 string score = entry.optstring("score", "");                                 boolean alive = entry.optboolean("alive", false);                                 integer shares = entry.optint("shares", 0);                                 log.d("json success", score );                             }                         }                     } catch (jsonexception e){                         log.e("json err", "failure converting json string", e);                     } 

error getting:

e2570 (5:10 pm): org.json.jsonexception: value {"worker.1":{"last_share":1443694390,"score":"15.6018529132","alive":true,"shares":59880,"hashrate":0},"worker.2":{"last_share":1443694180,"score":"2.97304689833","alive":true,"shares":2048,"hashrate":0},"worker.3":{"last_share":1440778690,"score":"0.0","alive":false,"shares":0,"hashrate":0},"ivonme.ant4":{"last_share":1443701343,"score":"8688.78118933","alive":true,"shares":203088,"hashrate":78633}} of type org.json.jsonobject cannot converted jsonarray @ org.json.json.typemismatch(json.java) @ org.json.jsonarray.(jsonarray.java) @ org.json.jsonarray.(jsonarray.java)

so have...

   {  //jsonobject       "worker.1":{            "last_share":1443639029,          "score":"3722204.62578",          "alive":true,          "shares":1124332,          "hashrate":1047253       },       "worker.2":{            "last_share":1443639029,          "score":"3794755.69049",          "alive":true,          "shares":1069332,          "hashrate":1012070       } //,...    } 

to workers have to...

try{    jsonobject jobject = new jsonobject(yourstringfromyourquestion);     // 1 specific element:    jsonobject worker1 = jobject.getjsonobject("worker.1");    jsonobject worker2 = jobject.getjsonobject("worker.2");     // iterate on elements    iterator<?> keys = jobject.keys();    while( keys.hasnext() ) {       string key = (string)keys.next(); // "worker.1", etc       if ( jobject.get(key) instanceof jsonobject ) {          jsonobject entry = jobject.getjsonobject(key);          // entry here like:          string score = entry.optstring("score", "");          boolean alive = entry.optboolean("alive", false);          integer shares = entry.optinteger("shares", 0);       }    } } catch (jsonexception e){    log.e(log_tag, "failure converting json string", e); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -