個々のアイテムを取得するためにJSONObject
を解析する必要がある以下のコードです。私がJSONを使って作業しているのは初めてです。そのため、JSONObject
から個々のアイテムを取得するには、JSONObject
を解析する方法がわかりません。個々のアイテムを取得するためにJSONObjectを反復する方法
try {
String url = service + version + method + ipAddress + format;
StringBuilder builder = new StringBuilder();
httpclient = new DefaultHttpClient();
httpget = new HttpGet(url);
httpget.getRequestLine();
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for (String line = null; (line = bufferedReader.readLine()) != null;) {
builder.append(line).append("\n");
}
JSONObject jsonObject = new JSONObject(builder.toString());
// Now iterate jsonObject to get Latitude,Longitude,City,Country etc etc.
}
} catch (Exception e) {
getLogger().log(LogLevel.ERROR, e.getMessage());
} finally {
bufferedReader.close();
httpclient.getConnectionManager().shutdown();
}
私のJSONは次のようになります。
{
"ipinfo": {
"ip_address": "131.208.128.15",
"ip_type": "Mapped",
"Location": {
"continent": "north america",
"latitude": 30.1,
"longitude": -81.714,
"CountryData": {
"country": "united states",
"country_code": "us"
},
"region": "southeast",
"StateData": {
"state": "florida",
"state_code": "fl"
},
"CityData": {
"city": "fleming island",
"postal_code": "32003",
"time_zone": -5
}
}
}
}
私は上記の目的からlatitude
、longitude
、city
、state
、country
、postal_code
を取得する必要があります。誰でも効率的に行う方法を提案することはできますか?
良い答え。この場合、 'Map'はうまく動作しますが、より一般的なものにするために、Map 'を実行します。 –
Tavo