hey guys, i'm still noob android-related coding , stackoverflow in general, don't harsh on me haven't been able find question asked on here already. :-/
so understand can convert json strings strings , handle json objects in general through org.json bundle in android, here's current situation:
i need take json string url (i'm able this) , make array. 2 arrays. framework i'm using runs on python , returns dict contains lists (arrays in python). however, displayed json object. here's example of getting url java code:
{"keywords": ["middle east", "syria"], "link": [["middle east", "http://www.google.com/#q=middle east"], ["syria", "http://www.google.com/#q=syria"]]}
as can see, it's dict of 2 indices. first 1 "keywords" has list , second 1 "link" contains list of lists. 2 lists (the first 1 , second multidimensional one) want able manipulate in java. i'm aware can use jsonarray, problem arrays stored in python dict, , android application not make jsonarray. guys have ideas of how can handle this? i'm pretty lost. here code getting actual json string (the url in code not accessible everyone, it's being served paste on machine):
static public void refreshfeed(){ try{ string url = "http://192.17.178.116:8080/getkw?nextline="+line; line++; httpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(url); httpresponse response; response = httpclient.execute(httpget); httpentity entity = response.getentity(); inputstream in = entity.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder sb = new stringbuilder(); string input = null; try { while ((input = reader.readline()) != null) { sb.append(input + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } string enter = sb.tostring(); feedentry add = new feedentry(enter); addnewentry(add); in.close(); } catch(malformedurlexception e){ e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
please note without jsonstring being made jsonarray. translates json object regular string added "feedentry" object.
mapping python dict json array ... more work you'd expect. it'd better make either json object or start list, can mapped straight json array. info on serializing between python , java.
here's code example create list structure in python, , grab in android application:
#!/usr/bin/python print "content-type: text/html\n\n" import json collections import defaultdict mystuff = list() mystuff.append( ('1', 'b', 'c', 'd') ) mystuff.append( ('2', 'f', 'g', 'h') ) stufflist = list() s in stufflist: d = {} d['a'] = s[0] d['b'] = s[1] d['c'] = s[2] d['d'] = s[3] stufflist.append(d) print json.write(stufflist)
and in android:
// convert string (sb string butter http response) json array. jsonarray jarray = new jsonarray(sb.tostring()); for(int = 0; < jarray.length(); i++){ // each item json object. jsonobject json_data = jarray.getjsonobject(i); // data object ... int = json_data.getint("a"); string b = json_data.getstring("b"); string c = json_data.getstring("c"); string d = json_data.getstring("d"); // whatever data ... }
Comments
Post a Comment