This json occurs when a destination or origin is outside of the uk, therefore not giving me any results! I need to check for this for a null check so i dont receive null pointer exception
JSON RESULT:
{
"destination_addresses" : [ "Durham, NC, USA" ],
"origin_addresses" : [ "Lancashire, UK" ],
"rows" : [
{
"elements" : [
{
"status" : "ZERO_RESULTS"
}
]
}
],
"status" : "OK" }
Code:
public static void extractJsonFromRequest(GoogleResponsePojo response) {
String destinationAddress =
response.getDestination_addresses().get(0);
String timeTaken =
response.getRows().get(0).getElements().get(0).getDuration().getText();
String originAddress = response.getOrigin_addresses().get(0);
System.out.println("It will take ** "+ timeTaken + " ** to walk
from " + originAddress + " to " + destinationAddress);
}
Google Reponse POJO which is structure of json:
public class GoogleResponsePojo {
private List<String> destination_addresses;
private List<String> origin_addresses;
private List<Rows> rows;
public List<String> getDestination_addresses() {
return destination_addresses;
}
public void setDestination_addresses(List<String> destination_addresses) {
this.destination_addresses = destination_addresses;
}
public List<String> getOrigin_addresses() {
return origin_addresses;
}
public void setOrigin_addresses(List<String> origin_addresses) {
this.origin_addresses = origin_addresses;
}
public List<Rows> getRows() {
return rows;
}
public void setRows(List<Rows> rows) {
this.rows = rows;
}
}
class Rows {
private List<Element> elements;
public List<Element> getElements() {
return elements;
}
public void setElements(List<Element> elements) {
this.elements = elements;
}
}
class Element {
private TextValue distance;
private TextValue duration;
public TextValue getDistance() {
return distance;
}
public void setDistance(TextValue distance) {
this.distance = distance;
}
public TextValue getDuration() {
return duration;
}
public void setDuration(TextValue duration) {
this.duration = duration;
}
}
class TextValue {
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;
}
}
I essentially need to just parse the json below so that i can say (if status != ZERO_RESULTS ) save the response else throw an exception! Probably easy but im struggling! Thanks so much!
Change GoogleResponsePojo to:
You can take
duration
as following:And I recommend use http-request built on apache http api. Its simple to use: