tags:

views:

96

answers:

3

hi

i know how to parse response like {\"screenTitle\":\"National Geographic\"} token.optString("screenTitle"); up to this ok.

but see this response

{
  "status": "OK",
  "routes": [ {
    "summary": "Southern Exp",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": -34.9257700,
          "lng": 138.5997300
        },
        "end_location": {
          "lat": -34.9106200,
          "lng": 138.5991300
        },

i want the lat and long value. how can i achive this.

+1  A: 

Nothing will be able to parse the string that you've provided, because it has syntax errors.

However, this is JSON. Follow the instructions of your favourite tool that supports JSON in Java.

Are you able to provide the exception that is being raised? Then I could write something more specific.

Tim McNamara
UserBB
JSONException: A JSONArray text must start with '[' at character 1 of { "status": "OK", "routes": [ { "summary": "I-40 W", "legs": [ {
UserBB
any help please.????
UserBB
Are you calling `json.getJSONArray("routes")`?
The Elite Gentleman
please see comment
UserBB
A: 

Ok, seeing that you have a JSON string (and you're possibly using JSON from here), if you want to return an array from JSON, do something like this:

JSONArray array = json.getJSONArray("routes");

Edit To get Geo-location, you will have to iterate through the array. In this example, I've taken the value from routs[0]/legs[0]/steps[0]/start_location

JSONArray array = json.getJSONArray("routes");

for (int i = 0; i < array.length(); i++) {
    JSONObject subJson = array.getJSONObject(i); //Assuming that in the array there's ONLY JSON objects
    JSONArray legs = subJson.getJSONArray("legs");

    //Now for legs, you can iterate through it but I don't know how your JSON structure is so
    //I didn't do it here.
    JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");
    JSONObject startLocation = steps.getJSONObject(0).getJSONObject("start_location");

    double lat = startLocation.getDouble("lat");
    double lng = startLocation.getDouble("lng");
}
The Elite Gentleman
yes its rightbut i only want lat and long which is under steps array.how i can achive that.
UserBB
See my edited post.
The Elite Gentleman
+1  A: 

Here's a quick code to get latitude and longitude of start_location in the following path routes[0]/legs[0]/steps[0]/start_location:

   JSONObject obj = new JSONObject(source.toString());
   JSONObject startLoaction = obj.getJSONArray("routes")
      .getJSONObject(0).getJSONArray("legs")
      .getJSONObject(0).getJSONArray("steps")
      .getJSONObject(0).getJSONObject("start_location");
   System.out.println(
     startLoaction.get("lat") + ", " + startLoaction.get("lng")
   );

This code is just to give you an idea as to how to parse objects in JSON deep down. You may want to write a generic path like query mechanism (think XPath-like) for JSON objects, which will give you objects based on query inside a JSON

e.g. In your case:

JSONObject startLocation = JSONFinder.query(
      "/routes[0]/legs[0]/steps[0]/start_location", sourceJSONObject)
naikus