Question

i want to get the value of the status element from geocode url.

{

    "results": [
        {
            "address_components": [
                {
                    "long_name": "India",
                    "short_name": "IN",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 35.5043404,
                        "lng": 97.395555
                    },
                    "southwest": {
                        "lat": 6.747138899999999,
                        "lng": 68.1623859
                    }
                },
                "location": {
                    "lat": 20.593684,
                    "lng": 78.96288
                },
                "location_type": "APPROXIMATE",
                "viewport": { … }
            },
            "types": [
                "country",
                "political"
            ]
        }
    ],
    "status": "OK"

}

i can get latitude using the following code.i tried to get status element, but still coudn't figure out how to do it.here i use gson.

URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=india&sensor=false");

  String output = new Scanner(url.openStream()).useDelimiter("\\A").next();
  JsonParser jsonParser = new JsonParser();
  JsonObject lat = jsonParser.parse(output).getAsJsonObject().getAsJsonArray("results").get(0).getAsJsonObject().getAsJsonObject("geometry").getAsJsonObject().getAsJsonObject("location");
   String latitude = lat.get("lat").getAsString();

please help.

Était-ce utile?

La solution

You could use

String status = jsonParser.parse(output).getAsJsonObject().get("status").getAsString();

But make sure to parse your output only once, i.e. store the result of jsonParser.parse(output).getAsJsonObject() in a local variable.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top