gpt4 book ai didi

android - 从 Google 获取 json 响应将 API 放置在 android 中

转载 作者:行者123 更新时间:2023-11-29 16:20:01 24 4
gpt4 key购买 nike

我的简单 android 应用程序使用 google places api 来获取地点的纬度和经度。我从 google api 得到 json 响应,如下所示

 {
"results" : [

{
"address_components" : [
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.23408510,
"lng" : 80.33229129999999
},
"southwest" : {
"lat" : 12.94592670,
"lng" : 80.14878270
}
},
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.14736150,
"lng" : 80.37764240
},
"southwest" : {
"lat" : 12.97345190,
"lng" : 80.12152360
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

现在我想在 json 响应中获取这部分,

"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
}

我通过这个方法只得到整个“几何”部分作为字符串

JSONObject e = address.getJSONObject(0);
e.getString("geometry")

如何使用唯一的位置值。任何帮助表示赞赏。我不确定出了什么问题。

最佳答案

JSONObject e = address.getJSONObject(0);
JSONObject jsonLocation = e.getJSONObject("geometry").getJSONObject("location");

并使用

String lat = jsonLocation.getString("lat");
String lng = jsonLocation.getString("lng");

获取纬度和经度。

关于android - 从 Google 获取 json 响应将 API 放置在 android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7566585/

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