我是android开发新手,在我的应用程序中,我使用google place api从地址中获取lat和long,但是我一直在解析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");
}