Google の Geocoding API を使用して、ジオコードの位置情報を含む JSON 文字列を取得しています。Google から返された文字列を次に示します。
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "550 Susong Dr, Morristown, TN 37814, USA",
"address_components": [ {
"long_name": "550",
"short_name": "550",
"types": [ "street_number" ]
}, {
"long_name": "Susong Dr",
"short_name": "Susong Dr",
"types": [ "route" ]
}, {
"long_name": "Morristown",
"short_name": "Morristown",
"types": [ "locality", "political" ]
}, {
"long_name": "Morristown",
"short_name": "Morristown",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Hamblen",
"short_name": "Hamblen",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "Tennessee",
"short_name": "TN",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "37814",
"short_name": "37814",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 36.2422740,
"lng": -83.3219410
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 36.2391264,
"lng": -83.3250886
},
"northeast": {
"lat": 36.2454216,
"lng": -83.3187934
}
}
}
} ] }
ただし、Java で次のコードを実行すると、「java.lang.ClassCastException: java.lang.String incompatible with net.sf.json.JSONObject」エラーが発生します。
URL url = new URL(URL + "&address=" + URLEncoder.encode(address, "UTF-8") + "&signature=" + key);
URLConnection conn = url.openConnection();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
IOUtils.copy(conn.getInputStream(), output);
output.close();
GAddress gaddr = new GAddress();
JSONObject json = JSONObject.fromObject(output.toString());
JSONObject placemark = (JSONObject) query(json, "Placemark[0]");
なぜエラーが発生するのかわかりません。Google の応答は、有効な JSON 文字列のように見えます。他の誰かがこれに問題がありましたか? なんらかの理由で Google との相性が悪い場合は、net.sf.json 以外のものを使用してもかまいません。
ありがとう、
アンドリュー