java - Android: How do I get the following JSON Object? -


i trying latitude , longitudes items location in json getting array out of bounds exception on android.

here json file:

{ "results" : [   {      "address_components" : [         {            "long_name" : "21211",            "short_name" : "21211",            "types" : [ "postal_code" ]         },         {            "long_name" : "baltimore",            "short_name" : "balt",            "types" : [ "locality", "political" ]         },         {            "long_name" : "maryland",            "short_name" : "md",            "types" : [ "administrative_area_level_1", "political" ]         },         {            "long_name" : "united states",            "short_name" : "us",            "types" : [ "country", "political" ]         }      ],      "formatted_address" : "baltimore, md 21211, usa",      "geometry" : {         "bounds" : {            "northeast" : {               "lat" : 39.3476939,               "lng" : -76.619315            },            "southwest" : {               "lat" : 39.310951,               "lng" : -76.65690289999999            }         },         "location" : {            "lat" : 39.3289463,            "lng" : -76.63838319999999         },         "location_type" : "approximate",         "viewport" : {            "northeast" : {               "lat" : 39.3476939,               "lng" : -76.619315            },            "southwest" : {               "lat" : 39.310951,               "lng" : -76.65690289999999            }         }      },      "place_id" : "chijn-z5wtqeyikrqx9ctdqf7-i",      "types" : [ "postal_code" ]   } ], "status" : "ok" } 

i need 2 attributes:

  "location" : {            "lat" : 39.3289463,            "lng" : -76.63838319999999         } 

i tried doing no luck:

        jsonobject jobj = new jsonobject(jdata);         jsonarray results = jobj.getjsonarray("results");         jsonobject geometry = results.getjsonobject(2); //error right here!         jsonobject  location = geometry.getjsonobject("location");         double latitude = location.getdouble("lat");         double longitude = location.getdouble("lng"); 

you cannot geomettry did,since there no object @ index 2 in json object. think have not formatted json responce. copy past responce in web site , ll understand.

and here code:

jsonobject tmp = results.getjsonobject(0); jsonobject geometery = tmp.getjsonobject ("geometery"); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -