4

JSON形式のGoogleGeocode応答を使用しています。

JSON形式は次のとおりです。

{
  "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を書きたくありません...

4

5 に答える 5

16

ジャクソンを使用する

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 ;
    }
于 2012-11-30T02:29:55.970 に答える
2

誰かが同じ質問をしている場合は、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;
}}

およびGsonAPIEx_

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

そしてそれを取得するためのこの関数:

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; 
}
于 2013-01-29T17:42:42.570 に答える
2

いつでもhttp://www.jsonschema2pojo.org/を使用できます。それはあなたのためにそれをします、そしてあなたはそれを手動でする必要はありません。


于 2014-04-04T04:46:47.843 に答える
2

質問はJSONのシリアル化と逆シリアル化について尋ねますが、あなたの本当の目標が何であるかは明確ではありません。Javaコードでジオロケーション情報を使用できるようにしたいだけの場合もあります。その場合、ほとんどすべてのジオロケーション情報APIにJavaSDK/クライアントが含まれていることをお勧めします。これがGoogleSmartyStreetsへのリンクです。これらは私がよく知っている2つのサービスです。

これは、Googleのリポジトリから直接コピーアンドペーストした例です。ご覧のとおり、データへのアクセスが非常に簡単になります。

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で働いてきました。)

于 2016-09-30T16:24:05.797 に答える
0

ジャクソンは最高です。romu31が提供するモデルクラスを利用し、ジャクソンライブラリをクラスパスに配置し、SpringRestTemplateを使用して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;
}

ジャクソンライブラリをクラスパスに配置するだけで、ジャクソンから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);

ただし、このソリューションには小さな問題があり、クラス名とプロパティ名は十分ではありません。それはどういうわけか悪い慣習です。クラス名とプロパティ名をより適切な規則にリファクタリングできることは知っていますが、データマーシャルロジックを実装するための一定の努力を意味します。

于 2016-08-25T07:00:13.880 に答える