我是安卓development.In的新手,我的应用程序我正在使用google从address.But中获取lat,我一直在解析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,谢谢
发布于 2015-02-10 05:01:29
你忘了几何学
jsonArray = jsonObject.getJSONArray("results");
for (int j = 0; j < jsonArray.length(); j++) {
jsonObject = jsonArray.getJSONObject(i);
latitude=jsonObject.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
} 发布于 2015-02-10 05:05:02
try {
JSONObject jObject = new JSONObject(response);
JSONArray jResults = jObject.getJSONArray("results");
JSONObject jPlaceObj = jResults.getJSONObject(0);
JSONObject jGeometry = jPlaceObj.getJSONObject("geometry");
JSONObject jLocation = jGeometry.getJSONObject("location");
String lat = jLocation.getString("lat");
String lng = jLocation.getString("lng");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}其中' response‘是来自的字符串中的JSON响应。如果您需要的话,我没有添加转换代码,请告诉我。
发布于 2015-02-10 05:05:25
根是这里的一个对象。所以,
JSONObject root=new JSONObject(result);// result is JSON string“结果”是一个数组。所以,
JSONArray resultArray=root.getJSONArray("results");你需要lat和lang的价值。所以,
JSONObject locationObj=resultArray.getJSONObect("location");
Double lat=locationObj.getDouble("lat");
Double lng=locationObj.getDouble("lng");如果结果只有一个位置,您可以直接使用上面的代码。如果结果中有许多地方,则创建一个Location模型类。宣布lat和lang为属性。并将值保存到位置的ArrayList中。
https://stackoverflow.com/questions/28424331
复制相似问题