android - How to differentiate value of array with two object with same key in java -
i implementing android app. have google spreadsheet 100 rows , 2 columns. fetching record of google spreadsheet in app. got json response :
json response
{ "version": "0.6", "reqid": "0", "status": "ok", "sig": "862971983", "table": { "cols": [ { "id": "a", "label": "name", "type": "string" }, { "id": "b", "label": "phoneno", "type": "number", "pattern": "general" } ], "rows": [ { "c": [ { "v": "a anil c agrawal" }, { "v": 7926605853, "f": "7926605853" } ] }, { "c": [ { "v": "a balaji agarwal" }, { "v": 8463225752, "f": "8463225752" } ] },.................
here have c array type 2 object same key want same keys in different variable ex. string name, string phoneno.
here method fetching google spreadsheet response , print it. if print value of name showing both name , phone number want name= abc , phoneno = 8574968521.
public void getspreadsheetdata() { string result = getspreadsheetresponce(); list<string> arraylist = new arraylist<string>(); if (result == null) { log.e("net not present:", ""); } else { int start = result.indexof("{", result.indexof("{") + 1); int end = result.lastindexof("}"); string jsonresponse = result.substring(start, end); try { jsonobject mainobj = new jsonobject(jsonresponse); string name = null; if (mainobj != null) { jsonarray list = mainobj.getjsonarray("rows"); if (list != null) { (int = 0; < list.length(); i++) { jsonobject innerjsonobject = list.getjsonobject(i); if (innerjsonobject != null) { // jsonarray valuesjsonarray = innerjsonobject .getjsonarray("c"); if (valuesjsonarray != null) { (int j = 0; j < valuesjsonarray .length(); j++) { jsonobject innerelem = valuesjsonarray .getjsonobject(j); if (innerelem != null) { name = innerelem.getstring("v"); arraylist.add(name); log.i("arraylist", "" + arraylist); } } } } } } } } catch (jsonexception e) { e.printstacktrace(); } } } private string getspreadsheetresponce() { string responce = " "; string urlstring = ""; try { string fetch_row_with_where_clause_url = "https://spreadsheets.google.com/tq?tq&key=1n_vjwhm8qiu5yrmm-wmkjiap2kza5vofxwi5dpyt1pe&gid=1262286779"; url urlspreadsheet = new url(fetch_row_with_where_clause_url); urlconnection urlconnection = urlspreadsheet.openconnection(); httpurlconnection connection = null; if (urlconnection instanceof httpurlconnection) { connection = (httpurlconnection) urlconnection; } else { system.out.println("please enter http url."); } bufferedreader in = new bufferedreader(new inputstreamreader( connection.getinputstream())); string current; while ((responce = in.readline()) != null) { urlstring += responce; system.out.println(urlstring); log.e("response::", "in loop:" + urlstring); } log.e("response::", "final response:" + urlstring); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return urlstring; }
is possible please me.
you not need array json object name. means have use here object key.
//jsonobject c = jsonobj.getjsonobject("row"); //convert jsonarray in json object iterator<string> = c.keys();// check first , next object while (it.hasnext()) { // call while have next object in array string key = it.next(); // next obj name in string "c" jsonobject keyobj = new jsonobject(c.tostring()); // gat "c" object array json
- get "rows" array in json object
- now not object "c" use here object key method
- check till "row" array has no object
- in json method call 2 time , data same object name
Comments
Post a Comment