android - Pull JSONObject from JSONArray? -
i'm trying pull "price" object "current" array, have been @ hours no luck, appreciated! :)
try { url url = new url("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=2"); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); try { bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(urlconnection.getinputstream())); stringbuilder stringbuilder = new stringbuilder(); string line; while ((line = bufferedreader.readline()) != null) { stringbuilder.append(line).append("\n"); } bufferedreader.close(); return stringbuilder.tostring(); } { jsonarray nu1 = jobj.getjsonarray("current"); jsonobject jobj = nu1.getjsonobject(0); string price = jobj.getstring("price"); toast.maketext(getapplicationcontext(), price, toast.length_short).show(); } } catch (exception e) { log.e("error", e.getmessage(), e); return null; } } protected void onpostexecute(string response) { } }
}
i tried response url. here response :
{ "item": { "icon": "http://services.runescape.com/m=itemdb_rs/1502782993572_obj_sprite.gif?id=2", "icon_large": "http://services.runescape.com/m=itemdb_rs/1502782993572_obj_big.gif?id=2", "id": 2, "type": "ammo", "typeicon": "http://www.runescape.com/img/categories/ammo", "name": "cannonball", "description": "ammo dwarf cannon.", "current": { "trend": "neutral", "price": 339 }, "today": { "trend": "positive", "price": "+1" }, "members": "true", "day30": { "trend": "positive", "change": "+1.0%" }, "day90": { "trend": "negative", "change": "-11.0%" }, "day180": { "trend": "negative", "change": "-21.0%" } } }
there no array in response.
edit:
assume store response in string named response
, can price, using following code:
jsonobject json = new jsonobject(response); jsonobject item = json.getjsonobject("item"); jsonobject current = item.getjsonobject("current"); int price = current.getint("price");
edit2: use
string response = stringbuilder.tostring();
and make jsonobject 'response' .
Comments
Post a Comment