我是 android 编程新手,我想知道如何从 JSONarray 获取某个对象。
我的 JSON 看起来像这样:
{"results" : [
{
"address_components" : [
{
"long_name" : "Contern",
"short_name" : "Contern",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Luxembourg",
"short_name" : "Luxembourg",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Luxembourg",
"short_name" : "LU",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Contern, Luxembourg",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 49.621830,
"lng" : 6.302790
},
"southwest" : {
"lat" : 49.56759010,
"lng" : 6.195380
}
},
"location" : {
"lat" : 49.58515930,
"lng" : 6.2274880
},
我想从位置中提取经纬度和经度。我的代码是:
arr = json.getJSONArray("results");
JSONObject location=arr.getJSONObject(4);
double lng = location.getDouble("lng");
double lat = location.getDouble("lat");