GSON (or) JSON to contentvalues with array of arrays -


i trying fusiontables sql response sqlite db in android. jsonlint.com says object valid json. but, arrays in "rows" not recognized jsonarrays or jsonarrays. number of rows in sql response can vary. sample response below. appreciate in pointing me in right direction solution. helping me ask better question. stumped!

{ "columns":"[text, number, location, date, ma, mb, mc, md, me, mf, mg, mh, mi, xtid, jtid]",  "kind":"fusiontables#sqlresponse",  "rows":"[ [text, 1.0, 33.2, -81.2, 2013-04-29t20:34:31.518-04:00, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 100.0],  [text, 3.0, 33.2, -81.2, 2013-04-29t20:43:43.257-04:00, 24.0, 23.0, 18.0, 19.0, 54.0, 21.0, 31.0, 45.0, 32.0, 29.7, 58.1],  [text, 5.0, 33.2, -81.2, 2013-05-01t06:58:09.801-04:00, 51.0, 51.0, 51.0, 51.0, 51.0, 51.0, 51.0, 51.0, 51.0, 51.0, 100.0],  [text, 3.0, 33.2, -81.2, 2013-05-02t05:32:04.970-04:00, 52.0, 52.0, 52.0, 52.0, 52.0, 52.0, 52.0, 52.0, 52.0, 52.0, 100.0] ]"  }   //************ code string json = syncresponse.tostring(); jsonobject root = parser.parse(json).getasjsonobject(); system.out.println("root" + root);  //this got json output listed above 

the value in rows not recognized array because it's not array!

you have double quotes before , after square brackets: "[...]", that's say, have string... string contains representation of json array, can't seen array...

a correct json response in case should following. note values strings written double quotes, arrays (and numbers) not surronded double quotes...

{     "columns": [ "text", "number", "location", "date", "ma", "mb", "mc", "md", "me", "mf", "mg", "mh", "mi", "xtid", "jtid" ],     "kind": "fusiontables#sqlresponse",     "rows": [         [ "text", 1, 33.2, -81.2, "2013-04-29t20:34:31.518-04:00", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 100],         [ "text", 3, 33.2, -81.2, "2013-04-29t20:43:43.257-04:00", 24, 23, 18, 19, 54, 21, 31, 45, 32, 29.7, 58.1 ],         [ "text", 5, 33.2, -81.2, "2013-05-01t06:58:09.801-04:00", 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 100],         [ "text", 3, 33.2, -81.2, "2013-05-02t05:32:04.970-04:00", 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 100]     ] } 

edit: first of all, note said before, json response in not valid... well, better said, it's valid it's not representing json elements correctly, it's representing them strings...

that said, came following workaround:

first of need class wrap json response, in case like:

public class response {     public string columns;     public string kind;     public string rows; } 

then have parse response (assuming have json response in string jsonstring):

gson gson = new gson(); response response = gson.fromjson(jsonstring, response.class); 

now have class response 3 attributes representing 3 strings in json response (columns, kind , rows).

now have re-parse strings, this:

type listofstringstype = new typetoken<list<string>>() {}.gettype(); list<string> columns = gson.fromjson(response.columns, listofstringstype);  

with this, have list<string> columns containing values: "text", "number", "location", "date", "ma", ...

lastly have somethig similar columns, in case it's not list, list of lists:

type listoflistsofstringstype = new typetoken<list<list<string>>>() {}.gettype(); list<list<string>> rows = gson.fromjson(response.rows.replace(":", "-"), listoflistsofstringstype); 

note time i've added .replace(":", "-") because otherwise you'll error, because : interpreted gson special character (see this).

once you've done of this, you'll have values in lists of strings, , you'll able parse these values , store them in classes wish...

obviously not beautiful way, it's workaround taking account difficulty of situation...


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -