Search code examples
javajsongoogle-fusion-tables

Parsing Google Fusion Table JSON


I have test-table, which when retrieved by SQL-query URL gives this:

{
 "kind": "fusiontables#sqlresponse",
 "columns": [
  "index",
  "name",
  "latitude",
  "longitude",
  "address"
 ],
 "rows": [
  [
   "1",
   "Shop1",
   "28.575326",
   "77.32237",
   "Sector 19"
  ],
  [
   "2",
   "Shop2",
   "43.34343",
   "44.34343",
   "Setor 18"
  ]
 ]
}

I am unable to parse this Json. It expects a JSOn object at start of array:

"rows": [

How do I parse this kind of JSON? In all the examples I have seen, it expects a key_value pair and objects(starting with "{") inside array not "[".

I use following code to parse:

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
    // Getting Array of Contacts
    contacts = json.getJSONArray(TAG_ROWS);

    // looping through All Contacts
    for(int i = 0; i < contacts.length(); i++){
        JSONObject c = contacts.getJSONObject(i);

        // Storing each json item in variable
        String id = c.getString(TAG_INDEX);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

It throws JSONEXception at line

    JSONObject c = contacts.getJSONObject(i);

Solution

  • Or is this the best approach to solve this:

    http://code.google.com/p/google-api-java-client/wiki/APIs#Fusion_Tables_API

    A client-side Java library from Google should work best.