Wednesday, 15 September 2010

java - dont know how to retrieve JSON data in android (this in particular) -



java - dont know how to retrieve JSON data in android (this in particular) -

i been reading , searching on net, , still can find best(easiest) way retrieve json info, if u inquire me, construction of messy coz id of blocks, info need obtain... =/

{"blocks": {"305795": {"is_mature": 1, "total_score": 1, "mining_duration": "1:39:26", "date_found": "2014-06-14 18:25:27", "confirmations": 1, "total_shares": 1, "date_started": "2014-06-14 16:46:01", "reward": "1", "nmc_reward": "0e-8"}, "305796": {"is_mature": 1, "total_score": 1, "mining_duration": "6:17:00", "date_found": "2014-06-15 11:29:32", "confirmations": 1, "total_shares": 1, "date_started": "2014-06-15 05:12:32", "reward": "1", "nmc_reward": "0e-8"} }

the problem have, need block number field same way need other keys... create basic table info rows...

i have atm.

jsonobject json_blocks = json.getjsonobject("blocks"); //log.e("json debug", "number of blocks: " + string.valueof(json_blocks.length())); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // looping through contacts iterator iter = json_blocks.keys(); while(iter.hasnext()){ string key = (string)iter.next(); string value = (string) json_blocks.getstring(key); system.out.println("key: "+key+" value: "+value); map.put(key,value); }

but fields within blocks huge string =( please, can give me tips?... thanks!

instead of doing json_blocks.getstring(key) should utilize json_blocks.opt(key) returns object. here can check if returned value instance of jsonobject. if is, cast it, , additional calls need data.

alternativley, can effort json_blocks.optjsonobject(key). if value returned (non null) cast jsonobject , process it, otherwise getstring phone call if that's want.

java android json

No comments:

Post a Comment