开发者

Android - How to parse JSONObject and JSONArrays

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 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜