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.
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.
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)
Use Following depedency
<!-- json -->
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Use following Code in your main method
public static void main(String[] args) {
try {
String point = "your json response";
Gson gson = new Gson();
TypeToken<RoutePlot> tokenPoint = new TypeToken<RoutePlot>() {
};
RoutePlot user = gson.fromJson(point, tokenPoint.getType());
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
Now use the following classes , i have generated for response to map into java
package com.admin.modules.restservices.google.pojo.routepojo;
public class Bounds {
private Northeast northeast;
private Southwest southwest;
public Northeast getNortheast() {
return northeast;
}
public void setNortheast(Northeast northeast) {
this.northeast = northeast;
}
public Southwest getSouthwest() {
return southwest;
}
public void setSouthwest(Southwest southwest) {
this.southwest = southwest;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Distance {
private String text;
private String value;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Duration {
private String text;
private String value;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Endlocation {
private String lat;
private String lng;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
import java.util.List;
public class Legs {
private Distance distance;
private Duration duration;
private String end_address;
private Endlocation end_location;
private String start_address;
private Startlocation start_location;
private List<Steps> steps;
private List<String> via_waypoint;
public List<String> getVia_waypoint() {
return via_waypoint;
}
public void setVia_waypoint(List<String> via_waypoint) {
this.via_waypoint = via_waypoint;
}
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public String getEnd_address() {
return end_address;
}
public void setEnd_address(String end_address) {
this.end_address = end_address;
}
public Endlocation getEnd_location() {
return end_location;
}
public void setEnd_location(Endlocation end_location) {
this.end_location = end_location;
}
public String getStart_address() {
return start_address;
}
public void setStart_address(String start_address) {
this.start_address = start_address;
}
public Startlocation getStart_location() {
return start_location;
}
public void setStart_location(Startlocation start_location) {
this.start_location = start_location;
}
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Northeast {
private String lat;
private String lng;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Overviewpolyline {
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Polyline {
private String points;
public String getPoints() {
return points;
}
public void setPoints(String points) {
this.points = points;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
import java.util.List;
public class RoutePlot {
private String status;
private List<Routes> routes;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Routes> getRoutes() {
return routes;
}
public void setRoutes(List<Routes> routes) {
this.routes = routes;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
import java.util.List;
public class Routes {
private Bounds bounds;
private String copyrights;
private List<Legs> legs;
private Overviewpolyline overview_polyline;
private String summary;
private List<String> warnings;
private List<String> waypoint_order;
public Bounds getBounds() {
return bounds;
}
public void setBounds(Bounds bounds) {
this.bounds = bounds;
}
public String getCopyrights() {
return copyrights;
}
public void setCopyrights(String copyrights) {
this.copyrights = copyrights;
}
public List<Legs> getLegs() {
return legs;
}
public void setLegs(List<Legs> legs) {
this.legs = legs;
}
public Overviewpolyline getOverview_polyline() {
return overview_polyline;
}
public void setOverview_polyline(Overviewpolyline overview_polyline) {
this.overview_polyline = overview_polyline;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public List<String> getWarnings() {
return warnings;
}
public void setWarnings(List<String> warnings) {
this.warnings = warnings;
}
public List<String> getWaypoint_order() {
return waypoint_order;
}
public void setWaypoint_order(List<String> waypoint_order) {
this.waypoint_order = waypoint_order;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Southwest {
private String lat;
private String lng;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
public class Startlocation {
private String lat;
private String lng;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
package com.admin.modules.restservices.google.pojo.routepojo;
import java.util.List;
public class Steps {
private Distance distance;
private Duration duration;
private Endlocation end_location;
private Startlocation start_location;
private String html_instructions;
private String travel_mode;
private Polyline polyline;
}
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");
}