gpt4 book ai didi

android - 解析 google place api json

转载 作者:太空狗 更新时间:2023-10-29 13:21:19 26 4
gpt4 key购买 nike

我是 android 开发的新手。在我的应用程序中,我使用 google place api 从地址获取纬度和经度。但是我一直在解析 json,因为我不知道如何解析这种 json。

JSON

{
"results": [
{
"address_components": [
{
"long_name": "Kurla West",
"short_name": "Kurla West",
"types": [
"sublocality_level_2",
"sublocality",
"political"
]
},
{
"long_name": "Mumbai",
"short_name": "Mumbai",
"types": [
"locality",
"political"
]
},
{
"long_name": "Mumbai Suburban",
"short_name": "Mumbai Suburban",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Maharashtra",
"short_name": "MH",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "India",
"short_name": "IN",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Kurla West, Mumbai, Maharashtra, India",
"geometry": {
"bounds": {
"northeast": {
"lat": 19.0894645,
"lng": 72.89236650000001
},
"southwest": {
"lat": 19.0587919,
"lng": 72.87059769999999
}
},
"location": {
"lat": 19.0707532,
"lng": 72.8781322
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 19.0894645,
"lng": 72.89236650000001
},
"southwest": {
"lat": 19.0587919,
"lng": 72.87059769999999
}
}
},
"types": [
"sublocality_level_2",
"sublocality",
"political"
]
}
],
"status": "OK"
}

我试过的代码。

jsonObject = new JSONObject(response);

if (jsonObject.get("results") instanceof JSONObject) {


} else if (jsonObject.get("results") instanceof JSONArray) {
jsonArray = jsonObject.getJSONArray("results");

for (int j = 0; j < jsonArray.length(); j++) {
jsonObject = jsonArray.getJSONObject(i);

latitude = jsonObject.getJSONObject("location").getDouble("lat");
}
}



} catch (JSONException f) {

Log.e("Json exception", f.getMessage());


}

异常

No value for location

请帮我解析这个json,谢谢

最佳答案

你忘了几何

jsonArray = jsonObject.getJSONArray("results");
for (int j = 0; j < jsonArray.length(); j++) {
jsonObject = jsonArray.getJSONObject(i);

latitude=jsonObject.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
}

关于android - 解析 google place api json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424331/

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