gpt4 book ai didi

java - 如何从 Map 解析嵌套的 JSON

转载 作者:行者123 更新时间:2023-11-30 07:58:21 27 4
gpt4 key购买 nike

我正在使用下面的方法将 json 响应映射到 Map

 Map<String, Object> apiResponse = restTemplate.postForObject("https://maps.googleapis.com/maps/api/geocode/json?address="+defaultLocation+"&key="+API_KEY, httpEntity, Map.class, Collections.EMPTY_MAP);

我可以使用下面的代码将整个 JSON 输出到一个字符串

    String jsonResponse = apiResponse.get("results").toString();

然而,我想要得到的是一个嵌套值,它是 results->geometry->location

我尝试了一些使用 JSONArrays、JSONObjects、Substring 的解决方案,但无法让它们工作。

响应 JSON:

{
"results" : [
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Auckland, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
}
},
"place_id" : "ChIJ--acWvtHDW0RF5miQ2HvAAU",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
},
{
"long_name" : "1010",
"short_name" : "1010",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Auckland, 1010, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
}
},
"place_id" : "ChIJuZqpSPtHDW0R4LOiQ2HvAAU",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
}
],
"status" : "OK"
}

如有任何帮助,我们将不胜感激。

最佳答案

JSONObject obj=new JSONObject(jsonresult);
// get result array
JSONArray resultsarray= obj.getJSONArray("results");
for (int i=0;i<resultsarray.length(),i++){
// get Objects using index
JSONObject jsonobject= results.getJSONObject(i);
// get geometry object
JSONObject geometry= jsonobject.getJSONObject("geometry");
// get location object from geometry
JSONObject location= geometry.getJSONObject("location");

// get location values from location object
double lat = location.optDouble("lat",0.0);
double long = location.optDouble("lng",0.0);
}

关于optDouble

public double optDouble(String key, double defaultValue) {

Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.

关于java - 如何从 Map<String, Object> 解析嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com