javascript Android - 如何解析 JSONObject 和 JSONArrays

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6350540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:22:54  来源:igfitidea点击:

Android - How to parse JSONObject and JSONArrays

javajavascriptandroidgoogle-api

提问by Chris Urquhart

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

我的版本是 Android 2.2 Google API 8 ,我从模拟器运行。我正在尝试在此 JSON 对象中尝试访问 Location long。我使用后得到这个

 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.

我需要在 location 字段中访问 lat 和 lon 并存储为双打。

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

回答by citizen conn

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());

That should give you lat/lng for northeast as doubles. Southeast is the same just replace northeast for southeast.

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

回答by nicholas.hauschild

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.

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