0

I am trying to access JSONObjects in this Image link to the JSON code

I am trying to access the "lat" and "lng" as such:

"geometry": {

    "location": {

        "lat": 37.422258,

        "lng": -122.0840678

    }
}

I am using Java in this case. I tried using this:

JSONObject dataObj = obj.getJSONObject("location");
lat = dataObj.getString("lat");
lng = dataObj.getString("lng");

But I got no result. Any help would be appreciated.

General Grievance
  • 4,555
  • 31
  • 31
  • 45

3 Answers3

1

37.422258 is not in quotes, so it's interpreted as a double not a string. Try calling getDouble.

Luke Usherwood
  • 3,082
  • 1
  • 28
  • 35
0

you can get a answer from this post.. looks like a duplicate question:

How to convert String to JSONObject in Java

hope it will help you to get some idea....

Community
  • 1
  • 1
Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43
0

I asked you to show your full JSON because the picture is blurry and it is always good to show the text but here is how you parse the JSON

JSONObject dataObj = new JSONObject(jsonString);
JSONArray results = dataObj.getJSONArray("results");
JSONObject geometry = results.getJSONObject(0).getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
lat = String.valueOf(location.getDouble("lat"));
lng = String.valueOf(location.getDouble("lng"));
meda
  • 45,103
  • 14
  • 92
  • 122