Question

I am working on an android app that makes a request to google places (details) and gets a json object in return. Here is the object https://developers.google.com/places/documentation/details#PlaceDetailsResponses

`{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "48",
            "short_name" : "48",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Pirrama Road",
            "short_name" : "Pirrama Road",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Pyrmont",
            "short_name" : "Pyrmont",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "NSW",
            "short_name" : "NSW",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "AU",
            "short_name" : "AU",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "2009",
            "short_name" : "2009",
            "types" : [ "postal_code" ]
         }
      ],
      "events" : [
        {
          "event_id" : "9lJ_jK1GfhX",
          "start_time" : 1293865200,
          "summary" : "<p>A visit from author John Doe, who will read from his latest book.</p>
                       <p>A limited number of signed copies will be available.</p>",
          "url" : "http://www.example.com/john_doe_visit.html"
        }
      ], ETC`

How can I get a specific detail from that json. If I try json.getJSONArray("result") it is telling me json is not from that type or if I try json.getString("results") it says no value for results. So how could I extract the information for example for short_name, etc?

Thanks in advance

Was it helpful?

Solution

You should try like

json.getJSONObject("result");

How to get short_name

JSONObject json=new JSONObject("json");
            JSONObject result=json.getJSONObject("result");
            JSONArray array=result.getJSONArray("address_components");
            for(int i=0;i<array.length();i++)
            {
                JSONObject obj = array.getJSONObject(i);
                String short_name=obj.getString("short_name");
                System.out.println("Short Name:"+short_name);
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top