java - how to code json array without array name? -
i'm new json android java eclipse. doing listview images , parsing json array. followed tutorial: http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/ . in tutorial, json array contains array name.however, mine doesn't contain array name. question how code json array without array name?
below json code.
[ { "event_id": "ev00000001", "event_title": "movie 1", }, { "event_id": "ev00000002", "event_title": "movie2", } ]
below json coding parsing json.
protected boolean doinbackground(string... urls) { try { httpget httppost = new httpget(urls[0]); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(httppost); // statusline stat = response.getstatusline(); int status = response.getstatusline().getstatuscode(); if (status == 200) { httpentity entity = response.getentity(); string data = entityutils.tostring(entity); jsonobject jsono = new jsonobject(data); jsonarray jarray = jsono.getjsonarray("actors"); (int = 0; < jarray.length(); i++) { jsonobject object = jarray.getjsonobject(i); events event = new events(); event.setevent_title(object.getstring("event_title")); eventlist.add(event); } return true; } //------------------>> } catch (parseexception e1) { e1.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return false; }
what suppose do?
this problem solved. thus, posting correct code.
protected boolean doinbackground(string... urls) { try { //------------------>> httpget httppost = new httpget(urls[0]); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(httppost); // statusline stat = response.getstatusline(); int status = response.getstatusline().getstatuscode(); if (status == 200) { httpentity entity = response.getentity(); string data = entityutils.tostring(entity); jsonarray array = new jsonarray(data); (int = 0; < array.length(); i++) { jsonobject obj = array.getjsonobject(i); events event = new events(); event.setevent_title(obj.getstring("event_title")); eventlist.add(event); } return true; } //------------------>> } catch (parseexception e1) { e1.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return false; }
here little example:
jsonarray array = new jsonarray(); jsonobject obj1 = new jsonobject(); obj1.put("key", "value"); array.put(obj1); jsonobject obj2 = new jsonobject(); obj2.put("key2", "value2"); array.put(obj2);
that looks like:
[ { "key": "value" }, { "key2": "value2" } ]
if want information jsonobjects in jsonarray iterate on them:
for (int = 0; < array.length(); i++) { jsonobject object = (jsonobject) array.get(i); object.get("event_id"); object.get("event_title"); }
Comments
Post a Comment