Parse google map JSON result using “javax.json” package

Java code:

import java.io.IOException;

import java.io.InputStream;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonString;

 

public class MapMain {

static InputStream is;
static JsonReader rdr;
static JsonObject obj,lJsonObj;
static JsonArray results;
static GeocodeImplementation geocodeImplementation;
static JsonNumber longitude;
static JsonNumber latitude;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
geocodeImplementation = new GeocodeImplementation();
String[] addresses = {
“1600 Amphitheatre Parkway, Mountain View, CA 94043, USA”,
“Forum Mall No. 21 Hosur Road Bengaluru Karnataka 560095”,
“RMZ Ecospace Marathahalli-Sarjapur Outer Ring Rd Adarsh Palm Retreat Bellandur Bangalore Karnataka 560103”,
“Sony World 100 Feet Rd Koramangala 4 Block Ejipura Bangalore Karnataka 560095”,
“Brigade Rd Shanthala Nagar Ashok Nagar Bangalore Karnataka”,
“Talakadu Jaladhama Resorts No-37 s1 II floor Basco court Gandhi Bazaar main road Basavanagudi Bangalore Karnataka 560004”,
“tanishq 11th Main Rd Jayanagar 3 East Block KV Layout Jayanagar Bangalore Karnataka 560011”,
“Wrong address”
};

for(int i=0;i<addresses.length;i++){
System.out.println(“================ ” + addresses[i] + ” ================”);
is = geocodeImplementation.getJSONByGoogle(addresses[i]);
rdr = Json.createReader(is);
obj = rdr.readObject();
results = obj.getJsonArray(“results”);
JsonString resultStatus;
resultStatus = obj.getJsonString(“status”);
System.out.print(resultStatus);
System.out.println(“: “);
System.out.println(“———–“);

for (JsonObject result : results.getValuesAs(JsonObject.class)) {
lJsonObj = result.getJsonObject(“geometry”).getJsonObject(“location”);
longitude = lJsonObj.getJsonNumber(“lng”);
latitude = lJsonObj.getJsonNumber(“lat”);
System.out.println(“longitude: ” + longitude + ” latitude: ” + latitude);

}
}
}

}

 

Sample JSON and its output from code above:

code_output

json

References:

Jar: http://www.java2s.com/Code/Jar/j/Downloadjavaxjson10jar.htm

Code Sample: http://www.oracle.com/technetwork/articles/java/json-1973242.html