Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most Efficient Way to Parse JSON Using Android

Tags:

json

android

I've written some code to parse the Google Distance Matrix JSON response received by my Android program. The only piece of data I'm interested in is in the "distance" "value" node.

My code works, but it seems like there must be an easier way to do this. The distance value node is nested pretty deep inside the JSON, but is it really necessary to go through every layer of the JSON to get to the field you want?

Here's my JSON response:

{
"destination_addresses" : [
  "5660 Baltimore National Pike, Ingleside Shopping Center, Catonsville, MD 21228, USA"
],
"origin_addresses" : [ "Hilltop Cir, Baltimore, MD 21250, USA" ],
"rows" : [
  {
     "elements" : [
        {
           "distance" : {
              "text" : "3.1 mi",
              "value" : 4922 <--THE FIELD I WANT TO EXTRACT
           },
           "duration" : {
              "text" : "11 mins",
              "value" : 666
           },
           "status" : "OK"
        }
     ]
  }
],
"status" : "OK"
}

And here is the code I used to pull out the distance value:

    private double extractDistance(JSONObject json) {
    JSONArray rowsArray = null;
    double distanceInMiles = -1;
    try {
        // Getting Array of Distance Matrix Results
        rowsArray = json.getJSONArray("rows");
        JSONObject rowsObject = rowsArray.getJSONObject(0);//only one element in this array
        JSONArray elementsArray = rowsObject.getJSONArray("elements");
        JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
        JSONObject distanceObject = elementsObject.getJSONObject("distance");
        distanceInMiles = (distanceObject.getDouble("value"))/1609.344; //distance in meters converted to miles
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return distanceInMiles;
}

Thanks!

like image 268
pjd Avatar asked Mar 12 '12 15:03

pjd


People also ask

What is the fastest JSON parser?

Performance results To our knowledge, simdjson is the first fully-validating JSON parser to run at gigabytes per second (GB/s) on commodity processors. It can parse millions of JSON documents per second on a single core.

What is the best and quickest way to parse JSON from the network?

try { JSONObject jsonObject = new JSONObject(output); JSONArray jsonArray = jsonObject. getJSONArray("marcadores"); } catch (JSONException e) { e. printStackTrace(); } } }). execute();

How efficient is JSON parse?

Parsing JSON is much more efficient than parsing object literals in JavaScript. This is true across all major JavaScript execution engines by up to 2x for an 8MB file, as demonstrated by this parsing benchmark.


1 Answers

I recommend you GSON (http://code.google.com/p/google-gson/) which parse the JSON text into a java class instance, and can convert the class instance to JSON text

like image 152
viplezer Avatar answered Sep 28 '22 11:09

viplezer