java - How to parse JSON data without array string? -
i trying parse data json json data array without string name. here example:
[ { "$id": "1", "clubvideoid": 1027, "clubid": 1, "title": "brian interview", "thumburl": "url", "videourl": "urll", "dateadded": "2014-03-25 00:00" }, { "$id": "2", "clubvideoid": 1028, "clubid": 1, "title": "chase interview", "thumburl": "url", "videourl": "urll", "dateadded": "2014-03-25 00:00" }, i can seem pass array without string. here code:
public void handleblogresponse() { mprogressbar.setvisibility(view.invisible); if (mvideodata == null) { updatedisplayforerror(); } else { try { jsonarray jsonposts = mvideodata.getjsonarray(""); arraylist<hashmap<string, string>> clubvideos = new arraylist<hashmap<string, string>>(); (int = 0; < jsonposts.length(); i++) { jsonobject post = jsonposts.getjsonobject(i); string thumburl = post.getstring(key_thumburl); thumburl = html.fromhtml(thumburl).tostring(); string dateurl = post.getstring(key_dateadded); dateurl = html.fromhtml(dateurl).tostring(); hashmap<string, string> blogpost = new hashmap<string, string>(); blogpost.put(key_thumburl, thumburl); blogpost.put(key_dateadded, dateurl); clubvideos.add(blogpost); } string[] keys = {key_thumburl, key_dateadded}; int[] ids = { android.r.id.text1, android.r.id.text2 }; simpleadapter adapter = new simpleadapter(this, clubvideos, android.r.layout.simple_list_item_2, keys, ids); setlistadapter(adapter); } catch (jsonexception e) { log.e(tag, "exception caught!!", e); } } } any suggestions?
here i'm pulling json:
@override protected jsonobject doinbackground(object... arg0) { int responsecode = -1; jsonobject jsonresponse = null; try { url blogfeedurl = new url("http://x.com/api/club/getx/1/?count=" + number_of_posts); httpurlconnection connection = (httpurlconnection) blogfeedurl.openconnection(); connection.connect(); responsecode = connection.getresponsecode(); if (responsecode == httpurlconnection.http_ok) { inputstream inputstream = connection.getinputstream(); reader reader = new inputstreamreader(inputstream); int contentlength = connection.getcontentlength(); char[] chararray = new char[contentlength]; reader.read(chararray); string responsedata = new string(chararray); jsonresponse = new jsonobject(responsedata); } else { log.i(tag, "unsuccessful http response code: " + responsecode); } log.i(tag, "code: " + responsecode); } catch (malformedurlexception e) { logexception(e); } catch (ioexception e) { logexception(e); } catch (exception e) { logexception(e); } return jsonresponse; }'
how loop through jsonarray?
jsonarray jr = new jsonarray("json string"); for(int i=0;i<jr.length();i++) { jsonobject jb = (jsonobject) jr.get(i); string id =jb.getstring("$id"); string clubvid = jb.getstring("clubvideoid"); ...// others } as squonk suggested
[ // json array node { // json object node "$id": "1", edit:
the below should in thread
@override protected string doinbackground(object... arg0) { string _response=null; try { httpclient httpclient = new defaulthttpclient(); httpclient.getparams().setparameter(coreprotocolpnames.protocol_version, httpversion.http_1_1); httpget request = new httpget("http://sql.gamedayxtra.com/api/club/getclubvideos/1"); httpresponse response = httpclient.execute(request); httpentity resentity = response.getentity(); _response=entityutils.tostring(resentity); }catch(exception e) { e.printstacktrace(); } return _response; } in onpostexecute
@override protected void onpostexecute(string _response) jsonarray jr = new jsonarray("_response"); for(int i=0;i<jr.length();i++) { jsonobject jb = (jsonobject) jr.get(i); string id =jb.getstring("$id"); string clubvid = jb.getstring("clubvideoid"); ...// others }
Comments
Post a Comment