gpt4 book ai didi

java - 在游戏中使用已解析的 json!框架

转载 作者:行者123 更新时间:2023-11-29 03:49:41 25 4
gpt4 key购买 nike

我想从 json 中获取经度和纬度。为此,我使用 Google Maps API 获取 json,然后将其放入 JsonElement,但现在我不知道如何读取这些值(纬度、经度)。

public static void getGeo(){
HttpResponse res = WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true").get();
int status = res.getStatus();
String type = res.getContentType();

JsonElement json = res.getJson();
JsonObject jsonObject = json.getAsJsonObject();
//with a system out i can see that the json is parsed correctly
System.out.print(jsonObject.get("results"));
}

解析后的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
}
}
}
} ]
}

有什么建议吗?

最佳答案

这应该可以解决问题:

HttpResponse res = WS.url("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true").get();

JsonElement json = res.getJson();
System.out.println("JSON: " + json);
//with a system out i can see that the json is parsed correctly
JsonArray jsArr = json.getAsJsonObject().getAsJsonArray("results");

for (JsonElement jsEl : jsArr)
{
JsonObject locationObject = jsEl.getAsJsonObject().getAsJsonObject("geometry").getAsJsonObject("location");
double lat = locationObject.getAsJsonPrimitive("lat").getAsDouble();
double lng = locationObject.getAsJsonPrimitive("lng").getAsDouble();

System.out.println("Lat is " + lat + " and lng is " + lng);
}

关于java - 在游戏中使用已解析的 json!框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9384760/

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