【问题标题】:Android - How to parse JSONObject and JSONArraysAndroid - 如何解析 JSONObject 和 JSONArrays
【发布时间】:2011-06-14 21:48:00
【问题描述】:

我的版本是 Android 2.2 Google API 8,我从模拟器运行。我正在尝试在此 JSON 对象中长时间访问位置。我用了之后就知道了

 InputStream instream = entity.getContent();

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

Google 文档说它返回一个数组,但带有花括号,看起来像一个对象。

我需要在位置字段中访问 lat 和 lon 并存储为双精度值。

我搜索过,但似乎只能找到简单文件的帮助。

{
   "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"
}

【问题讨论】:

    标签: java javascript android google-api


    【解决方案1】:
    JSONObject jObject = new JSONObject(convertStreamToString(instream));
    JSONArray results = jObject.getJSONArray("result");
    JSONObject geometry = results.getJSONObject(2);
    JSONObject bounds = geometry.getJSONObject("bounds");
    JSONObject northeast = geometry.getJSONObject("northeast");
    
    double nLat = Double.parseDouble(northeast.getString("lat").toString());
    double nLng = Double.parseDouble(northeast.getString("lng").toString());
    

    这应该给你东北的纬度/经度作为双打。东南是一样的,只是把东北换成东南。

    【讨论】:

    • JSONArray 结果中只有一个元素。
    【解决方案2】:
    JSONObject location = myAwway.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
    double lat = location.getDouble("lat");
    double lng = location.getDouble("lng");
    

    “结果”JSONArray 可能是 Google 文档建议的数组。他们刚刚将它包装在一个带有状态的 JSONObject 中,这样您就可以在尝试处理返回值之前检查状态。

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多