如何使用Java从Google geocode序列化和反序列化JSON对象
我正在处理谷歌地理代码响应,它们是JSON格式的 JSON格式如下所示:如何使用Java从Google geocode序列化和反序列化JSON对象,java,json,geocode,Java,Json,Geocode,我正在处理谷歌地理代码响应,它们是JSON格式的 JSON格式如下所示: { "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "address_components": [ { "long_name": "1600", "shor
{
"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
}
}
}
} ]
}
我正在尝试使用Java创建、序列化和反序列化它们。我尝试了GSON,但因为它不能在更深层次上反序列化对象,所以GSON将不是一个选项
我只是想知道是否有人有这方面的经验 话题?也许您尝试过一个可以解决这个问题的库?一些示例代码将非常棒
我真的不想为此编写自己的API…使用Jackson
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 ;
}
使用杰克逊
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 ;
}
如果有人有相同的问题,您可以使用romu31提供的GoogleGeoCodeResponse:
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;
}}
例如:
这个函数可以得到它:
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;
}
如果有人有相同的问题,您可以使用romu31提供的GoogleGeoCodeResponse:
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;
}}
例如:
这个函数可以得到它:
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;
}
你可以随时使用。
它为你做,你不必手动去做
您可以随时使用。 它为你做,你不必手动去做
Jackson是最好的,我利用了romu31提供的模型类,将Jackson库放在类路径中,并使用Spring RestTemplate直接获取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;
}
请注意,我只将jackson库放在类路径中,我甚至不需要执行jackson的任何API方法,请参阅下面的测试代码
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);
RestTemplate RestTemplate=new RestTemplate();
Map vars=newhashmap();
可变认沽权(“地址”、“香港”);
变量放置(“传感器”、“假”);
GeocodeResponse结果=restTemplate.getForObject(
"http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor={sensor}“,
GeocodeResponse.class,vars);
然而,这个解决方案有一个小问题,类名和属性名不够好。不知怎么的,这是一个坏习惯。
我知道我们可以将类名和属性名重构为更好的约定,但这意味着需要付出一定的努力来实现数据marhsall逻辑。Jackson是最好的,我利用了romu31提供的模型类,将Jackson库放在类路径中,并使用Spring RestTemplate直接获得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;
}
请注意,我只将jackson库放在类路径中,我甚至不需要执行jackson的任何API方法,请参阅下面的测试代码
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);
RestTemplate RestTemplate=new RestTemplate();
Map vars=newhashmap();
可变认沽权(“地址”、“香港”);
变量放置(“传感器”、“假”);
GeocodeResponse结果=restTemplate.getForObject(
"http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor={sensor}“,
GeocodeResponse.class,vars);
然而,这个解决方案有一个小问题,类名和属性名不够好。不知怎么的,这是一个坏习惯。
我知道我们可以将类名和属性名重构为更好的约定,但这意味着需要付出一定的努力来实现data marhsall逻辑。虽然问题是关于JSON序列化和反序列化的,但不清楚您的真正目标是什么。可能您只是希望能够在Java代码中使用地理位置信息,在这种情况下,我建议几乎所有地理位置信息API都有Java SDK/客户端。这里是链接和,这是我熟悉的两种服务 下面是一个直接从谷歌的回购协议粘贴的副本示例。如您所见,它使访问数据变得非常容易
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);
(完全披露:我为SmartyStreets工作过。)尽管问题是关于JSON序列化和反序列化的,但不清楚您的真正目标是什么。可能您只是希望能够在Java代码中使用地理位置信息,在这种情况下,我建议几乎所有地理位置信息API都有Java SDK/客户端。这里是链接和,这是我熟悉的两种服务 下面是一个直接从谷歌的回购协议粘贴的副本示例。如您所见,它使访问数据变得非常容易
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);
(完全公开:我为SmartyStreets工作过。)GSON绝对可以反序列化任意JSON。事实上。另请参见——Jackson是另一个强大的竞争者,可以以相当高的速度序列化/反序列化Java对象层次结构。我有很好的经验,非常感谢。最终决定使用杰克逊。解决的问题:pGoogleGeoCodeResponse结果=mapper.readValue(jsoninonesetring,GoogleGeoCodeResponse.class);GSON完全可以反序列化任意的JSON。另请参见——Jackson是另一个强大的竞争者,可以以相当高的速度序列化/反序列化Java对象层次结构。我有很好的经验,非常感谢。最终决定使用杰克逊。解决的问题:pGoogleGeoCodeResponse结果=mapper.readValue(jsoninonesetring,GoogleGeoCodeResponse.class);你可以添加一个更好/更解释的解决方案。我的是:你得到一个字符串输入,一个映射结果的类,你使用Jackson将输入反序列化到一个类中。你可以添加一个更好/更解释的解决方案。我的是:你得到一个字符串输入,映射结果的类,并使用Jackson将输入反序列化到类中。