I am having some trouble trying to figure out how to parse a line in a json file so that it only returns part of the line as a string. I will illustrate below:
public String GetDistance(String origin, String destination) throws MalformedURLException, IOException {
//URL url = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins" + origin + ",UK+destination=" + destination + ",UK&key=mykey");
URL url = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Cornwall,UK&destinations=London,UK&key=mykey");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String line, outputString = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
if (line.contains("distance")) {
outputString = reader.readLine().trim();
return outputString;
}
}
return outputString;
}
What this function does is create a json file in my browser using Google Maps API:
{
"destination_addresses" : [ "London, UK" ],
"origin_addresses" : [ "Cornwall, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "284 mi",
"value" : 456443
},
"duration" : {
"text" : "4 hours 52 mins",
"value" : 17530
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Currently the "outputString" returns the line: "text" : "284 mi". However, the desired output is to just return the miles, "284".
I know this is most likely a re post, however I have been searching around for a solution to this and have been unsuccessful in implementing something that works.
Any help on this would be greatly appreciated, Cheers.