Android的 - 如何分析的JSONObject和JSONArraysAndroid、JSONObject、JSONArrays

2023-09-04 06:01:35 作者:你近在咫尺,却远若天边

我的版本是Android 2.2的谷歌API 8,我可是从模拟器中运行。我想尝试访问位置长期在这个JSON对象。我得到这个之后,我用

 的InputStream河道= entity.getContent();

 JSONObject的myAwway =新的JSONObject(convertStreamToString(河道));
 

谷歌文档说,它返回一个数组,但与周围的花括号它看起来像一个对象。

我需要访问纬度和经度的位置字段和存储为双打。

香港专业教育学院搜查,但似乎只是寻求帮助与简单的文件。

  {
   结果:
      {
         address_components:[
            {
               long_name:20059,
               SHORT_NAME:20059,
               类型:postal_ code]
            },
            {
               long_name:华盛顿,
               SHORT_NAME:华盛顿,
               类型:地方,政治]
            },
            {
               long_name:哥伦比亚特区,
               SHORT_NAME:DC,
               类型:administrative_area_level_1,政治]
            },
            {
               long_name:美的,
               SHORT_NAME:美,
               类型:国,政治]
            }
         ]
         formatted_address:华盛顿特区20059,USA,
         几何 : {
            界限:{
               东北 : {
                  纬度:38.924920,
                  液化天然气:-77.0178720
               },
               西南 : {
                  纬度:38.9189910,
                  液化天然气:-77.02261200000001
               }
            },
            位置 : {
               纬度:38.92177780,
               液化天然气:-77.01974260
            },
            LOCATION_TYPE:近似,
            视口:{
               东北 : {
                  纬度:38.92510312068017,
                  液化天然气:-77.01709437931984
               },
               西南 : {
                  纬度:38.91880787931983,
                  液化天然气:-77.02338962068018
               }
            }
         },
         类型:postal_ code]
      }
   ]
   状态:OK
}
 

解决方案

 的JSONObject位置= myAwway.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
双纬度= location.getDouble(土地增值税);
双LNG = location.getDouble(LNG);
 
Android数据解析技术之JSONObject以及GSON应用实例

在'结果'JSONArray可能是数组谷歌文档建议。他们刚刚把它包在一个状态的JSONObject,这样你可以尝试工作,对返回值之前检查状态。

My build is Android 2.2 Google API 8 , Im running from the emulator. I am trying try access Location long in this JSON Object. I get this after I use

 InputStream instream = entity.getContent();

 JSONObject myAwway = new JSONObject(convertStreamToString(instream));

Google docs says it returns an array but with the surrounding curly braces It looks like an object.

I need to access lat and lon in the location field and store as doubles.

Ive searched but only seem to find help with simple files.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "20059",
               "short_name" : "20059",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Washington D.C.",
               "short_name" : "Washington D.C.",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "District of Columbia",
               "short_name" : "DC",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Washington D.C., DC 20059, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.924920,
                  "lng" : -77.0178720
               },
               "southwest" : {
                  "lat" : 38.9189910,
                  "lng" : -77.02261200000001
               }
            },
            "location" : {
               "lat" : 38.92177780,
               "lng" : -77.01974260
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.92510312068017,
                  "lng" : -77.01709437931984
               },
               "southwest" : {
                  "lat" : 38.91880787931983,
                  "lng" : -77.02338962068018
               }
            }
         },
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

解决方案

JSONObject location = myAwway.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
double lat = location.getDouble("lat");
double lng = location.getDouble("lng");

The 'results' JSONArray is probably the array Google docs suggested. They had just wrapped it up in a JSONObject with a status, so that you could check the status before attempting to work on the returned value.