Getting element of JSON object when key isn't the same [Android] -
i want learn bit more android , wanted create app track price of cryptocurrencies. choosed api: https://www.cryptocompare.com/api/#introduction
my problem following: when want list of coin json response looks this:
{ "response": "success", "message": "coin list succesfully returned!", "baseimageurl": "https://www.cryptocompare.com", "baselinkurl": "https://www.cryptocompare.com", "data": { "42": { "id": "4321", "url": "/coins/42/overview", "imageurl": "/media/19984/42.png", "name": "42", "coinname": "42 coin", "fullname": "42 coin (42)", "algorithm": "scrypt", "prooftype": "pow", "fullypremined": "0", "totalcoinsupply": "42", "preminedvalue": "n/a", "totalcoinsfreefloat": "n/a", "sortorder": "34" }, "365": { "id": "33639", "url": "/coins/365/overview", "imageurl": "/media/352070/365.png", "name": "365", "coinname": "365coin", "fullname": "365coin (365)", "algorithm": "x11", "prooftype": "pow/pos", "fullypremined": "0", "totalcoinsupply": "2300000000", "preminedvalue": "299000000", "totalcoinsfreefloat": "n/a", "sortorder": "916" }, (here url use (https://www.cryptocompare.com/api/data/coinlist/)
i want keep informations coin (everything "data") key isn't same.
how can informations create differents coins?
thank's in advance
you can use jsonobject#names() keys jsonarray , loop jsonarray.
jsonobject data = response.getjsonobject("data"); jsonarray array = data.names(); // contains keys inside data // loop array (int = 0; < array.length(); i++ ) { string key = array.getstring(i); // 42 or 365 example code jsonobject obj = data.getjsonobject(key); // contains jsonobject of key 42 or 365 } another way use jsonobject#keys() uses iterator , hasnext() iteration less performance efficient above normal for loop approach in android.
Comments
Post a Comment