GSON (or) JSON to contentvalues with array of arrays

I am trying to get a fusiontables sql response into an sqlite db in android. jsonlint.com says the object is valid JSON. But, the arrays in "rows" are not recognized as JsonArrays or JSONArrays. The number of rows in an sql response can vary. A sample response is below. I would appreciate any help in pointing me in the right direction for a solution. Even helping me to ask a better question. I am 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 is where I got the JSON output listed above

Answers


The value in rows is not recognized as an array because it's not an array!

You have double quotes before and after the square brackets: "[...]", that's to say, you have just a string... This string contains the representation of a JSON array, but it can't be seen as an array...

A correct JSON response in your case should be something like the following. Note that all the values that are strings are written with double quotes, but the arrays (and numbers) are not surronded by 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 that as I said before, your JSON response in not valid... well, better said, it's valid but it's not representing the JSON elements correctly, but it's representing them as just strings...

That said, I came up with the following workaround:

First of all you need a class to wrap your JSON response, in your case something like:

public class Response {
    public String columns;
    public String kind;
    public String rows;
}

Then you have to parse your response with (assuming you have your JSON response in a String jsonString):

Gson gson = new Gson();
Response response = gson.fromJson(jsonString, Response.class);

Now you have a class Response with 3 attributes representing the 3 strings in your JSON response (columns, kind and rows).

Now you have to re-parse those strings, like this:

Type listOfStringsType = new TypeToken<List<String>>() {}.getType();
List<String> columns = gson.fromJson(response.columns, listOfStringsType); 

With this, you have a List<String> columns containing all the values: "Text", "Number", "Location", "Date", "ma", ...

Lastly you have to do somethig similar with the columns, but in this case it's not a list, but a list of lists:

Type listOfListsOfStringsType = new TypeToken<List<List<String>>>() {}.getType();
List<List<String>> rows = gson.fromJson(response.rows.replace(":", "-"), listOfListsOfStringsType);

Note that this time I've added .replace(":", "-") because otherwise you'll get an error, because : is interpreted by GSON as an special character (see this).

Once you've done all of this, you'll have all your values in lists of strings, and you'll be able to parse these values and store them in your classes as you wish...

Obviously this is not the most beautiful way, but it's a good workaround taking into account the difficulty of the situation...


Need Your Help

Save inside loop

php cakephp

I created a function that has a infinite loop and a delay at the end to make sure it will only execute one time per second.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.