如何使用Java将JSON对象从Google地理编码序列化和反序列化

6 浏览
0 Comments

如何使用Java将JSON对象从Google地理编码序列化和反序列化

我正在处理Google Geocode的JSON响应。\nJSON格式如下:\n{\n \"status\": \"OK\",\n \"results\": [ {\n \"types\": [ \"street_address\" ],\n \"formatted_address\": \"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA\",\n \"address_components\": [ {\n \"long_name\": \"1600\",\n \"short_name\": \"1600\",\n \"types\": [ \"street_number\" ]\n }, {\n \"long_name\": \"Amphitheatre Pkwy\",\n \"short_name\": \"Amphitheatre Pkwy\",\n \"types\": [ \"route\" ]\n}, {\n \"long_name\": \"Mountain View\",\n \"short_name\": \"Mountain View\",\n \"types\": [ \"locality\", \"political\" ]\n}, {\n \"long_name\": \"California\",\n \"short_name\": \"CA\",\n \"types\": [ \"administrative_area_level_1\", \"political\" ]\n}, {\n \"long_name\": \"United States\",\n \"short_name\": \"US\",\n \"types\": [ \"country\", \"political\" ]\n}, {\n \"long_name\": \"94043\",\n \"short_name\": \"94043\",\n \"types\": [ \"postal_code\" ]\n} ],\n\"geometry\": {\n \"location\": {\n \"lat\": 37.4219720,\n \"lng\": -122.0841430\n },\n \"location_type\": \"ROOFTOP\",\n \"viewport\": {\n \"southwest\": {\n \"lat\": 37.4188244,\n \"lng\": -122.0872906\n },\n \"northeast\": {\n \"lat\": 37.4251196,\n \"lng\": -122.0809954\n }\n }\n}\n} ]\n}\n\n我希望使用Java来序列化和反序列化它们。我尝试了GSON,但由于它无法反序列化较深层次的对象,所以GSON不是一个选项。\n我想知道是否有人对此有经验?也许你已经尝试过可以解决这个问题的库?一些示例代码将非常棒。\n我真的不想自己编写API...

0
0 Comments

如何使用Java将Google地理编码的JSON对象进行序列化和反序列化

在开发过程中,我们经常需要将JSON对象序列化为字符串,或者将字符串反序列化为JSON对象。在这篇文章中,我们将讨论如何使用Java对Google地理编码的JSON对象进行序列化和反序列化。

问题的原因:

在我们的应用程序中,我们可能需要与Google地理编码服务进行交互,获取某个地点的地理编码信息。Google地理编码服务返回的结果是一个JSON对象。在某些情况下,我们可能需要将这个JSON对象保存到本地文件或数据库中,或者将它发送到其他系统。为了实现这些功能,我们需要将JSON对象序列化为字符串,或者将字符串反序列化为JSON对象。

解决方法:

在Java中,我们可以使用许多库来处理JSON对象的序列化和反序列化。其中一个常用的库是Google的Gson库。下面是使用Gson库对Google地理编码的JSON对象进行序列化和反序列化的示例代码:

1. 导入Gson库的依赖:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

2. 创建一个Java类来表示Google地理编码的JSON对象:

public class Geocode {
    private String formattedAddress;
    private String latitude;
    private String longitude;
    
    // getters and setters
}

3. 将JSON对象序列化为字符串:

Gson gson = new Gson();
Geocode geocode = new Geocode();
geocode.setFormattedAddress("1600 Amphitheatre Parkway, Mountain View, CA");
geocode.setLatitude("37.422");
geocode.setLongitude("-122.084");
String jsonString = gson.toJson(geocode);
System.out.println(jsonString);

4. 将字符串反序列化为JSON对象:

String jsonString = "{\"formattedAddress\":\"1600 Amphitheatre Parkway, Mountain View, CA\",\"latitude\":\"37.422\",\"longitude\":\"-122.084\"}";
Geocode geocode = gson.fromJson(jsonString, Geocode.class);
System.out.println(geocode.getFormattedAddress());
System.out.println(geocode.getLatitude());
System.out.println(geocode.getLongitude());

通过使用Gson库,我们可以轻松地将Google地理编码的JSON对象序列化为字符串,或者将字符串反序列化为JSON对象。这使得我们能够更方便地处理和传输地理编码信息。

在本文中,我们讨论了如何使用Java将Google地理编码的JSON对象进行序列化和反序列化。通过使用Gson库,我们可以轻松地实现这些功能。希望这篇文章能帮助你更好地理解JSON对象的序列化和反序列化过程,并在实际项目中应用它们。

0
0 Comments

如何使用Java从Google geocode中序列化和反序列化一个JSON对象

如果有人有相同的问题,你可以使用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;
    }
}

使用Gson API:

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);

获取JSON数据的函数:

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; 
}

以上就是如何使用Java从Google geocode中序列化和反序列化一个JSON对象的方法。

0
0 Comments

如何使用Java序列化和反序列化来自Google地理编码的JSON对象

问题起因:

我们需要将从Google地理编码API获取的JSON对象序列化为Java对象,并在需要时进行反序列化。这是因为JSON是一种常用的数据交换格式,而Java对象是我们在应用程序中常用的数据结构。通过将JSON对象转换为Java对象,我们可以更方便地对其进行处理和操作。

解决方法:

我们可以使用Jackson库来实现JSON对象的序列化和反序列化。以下是一个示例代码,演示如何使用Jackson库将JSON对象转换为Java对象:

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`类是我们用来映射JSON对象的Java类。它包含了与JSON对象对应的字段,以及与之相关联的其他自定义类。通过Jackson库的`readValue`方法,我们可以将JSON字符串`jsonInOneString`反序列化为`GoogleGeoCodeResponse`对象。

使用Jackson库可以方便地实现JSON对象的序列化和反序列化。通过将JSON对象转换为Java对象,我们可以更方便地对其进行处理和操作。以上示例代码演示了如何使用Jackson库将从Google地理编码API获取的JSON对象转换为Java对象。

0