How to serialize and deserialize a JSON object from Google geocode using Java

I work with Google Geocode answers that are in JSON.

The JSON format is as follows:

{ "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "address_components": [ { "long_name": "1600", "short_name": "1600", "types": [ "street_number" ] }, { "long_name": "Amphitheatre Pkwy", "short_name": "Amphitheatre Pkwy", "types": [ "route" ] }, { "long_name": "Mountain View", "short_name": "Mountain View", "types": [ "locality", "political" ] }, { "long_name": "California", "short_name": "CA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "94043", "short_name": "94043", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 37.4219720, "lng": -122.0841430 }, "location_type": "ROOFTOP", "viewport": { "southwest": { "lat": 37.4188244, "lng": -122.0872906 }, "northeast": { "lat": 37.4251196, "lng": -122.0809954 } } } } ] } 

I am trying to serialize and deserialize them using Java. I tried GSON, but since it cannot deserialize objects at a deeper level, GSON will not be an option.

I'm just wondering if anyone has experience in this topic? Perhaps you tried a library that can solve this problem? Some sample code would be awesome.

I really don't want to write my own API for this ...

+4
source share
5 answers

Jackson's use

 GoogleGeoCodeResponse result = mapper.readValue(jsonInOneString,GoogleGeoCodeResponse.class); public class GoogleGeoCodeResponse { public String status ; public results[] results ; public GoogleGeoCodeResponse() { } } class results{ public String formatted_address ; public geometry geometry ; public String[] types; public address_component[] address_components; } class geometry{ public bounds bounds; public String location_type ; public location location; public bounds viewport; } class bounds { public location northeast ; public location southwest ; } class location{ public String lat ; public String lng ; } class address_component{ public String long_name; public String short_name; public String[] types ; } 
+14
source

If anyone has the same question, you can use the GoogleGeoCodeResponse provided by romu31:

 public class GoogleGeoCodeResponse { public String status; public results[] results; public GoogleGeoCodeResponse() { } public class results { public String formatted_address; public geometry geometry; public String[] types; public address_component[] address_components; } public class geometry { public bounds bounds; public String location_type; public location location; public bounds viewport; } public class bounds { public location northeast; public location southwest; } public class location { public String lat; public String lng; } public class address_component { public String long_name; public String short_name; public String[] types; }} 

and Gson API Example:

  Gson gson = new Gson(); GoogleGeoCodeResponse result = gson.fromJson(jsonCoord(URLEncoder.encode(address, "UTF-8")); GoogleGeoCodeResponse.class); double lat = Double.parseDouble(result.results[0].geometry.location.lat); double lng = Double.parseDouble(result.results[0].geometry.location.lng); 

and this function:

 private String jsonCoord(String address) throws IOException { URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false"); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; String jsonResult = ""; while ((inputLine = in.readLine()) != null) { jsonResult += inputLine; } in.close(); return jsonResult; } 
+2
source

You can always use http://www.jsonschema2pojo.org/ . What is this for you and you do not need to do it manually.


+2
source

Although the question asks about serializing and deserializing JSON, it's not clear what your real goal is. Perhaps you just want to use geolocation information in Java code, in which case I would suggest that almost all geolocation APIs have Java SDK / clients. Here is a link on Google and on SmartyStreets , which are two services that I am familiar with.

Here's an example of copying and pasting directly from a Google repo. As you can see, it is very easy to access data.

 GeoApiContext context = new GeoApiContext().setApiKey("AIza..."); GeocodingResult[] results = GeocodingApi.geocode(context, "1600 Amphitheatre Parkway Mountain View, CA 94043").await(); System.out.println(results[0].formattedAddress); 

(Full disclosure: I worked at SmartyStreets.)

+1
source

Jackson is the best, I used the model class provided by romu31, put the Jackson library in the class path and used Spring RestTemplate to directly get the GeocodeResponse.

  public class GeocodeResponse { public String status; public results[] results; public GeocodeResponse() { enter code here } } class results { public String formatted_address; public geometry geometry; public String[] types; public address_component[] address_components; } class geometry { public bounds bounds; public String location_type; public location location; public bounds viewport; } class bounds { public location northeast; public location southwest; } class location { public String lat; public String lng; } class address_component { public String long_name; public String short_name; public String[] types; } 

Please note that I only put the jackson library in the classpath, I do not even need to execute any API method from jackson, see my test codes below

 RestTemplate restTemplate = new RestTemplate(); Map<String, String> vars = new HashMap<String, String>(); vars.put("address", "Hong Kong"); vars.put("sensor", "false"); GeocodeResponse result = restTemplate.getForObject( "http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor={sensor}", GeocodeResponse.class, vars); 

However, there is a slight problem with this solution; class names and property names are not good enough. This is somehow in poor agreement. I know that we can reorganize class names and property names into a better convention, but that will mean some effort to implement the marhsall data logic.

0
source

All Articles