Question

Here I want to get the types of a specific place using Google Places API, but Im obviously having a huge problem with how to get the desired value of "types" from the JSON format.

here is what I tried to do

 JSONObject predictions = new JSONObject(sb.toString()); 
             JSONArray ja = new JSONArray(predictions.getString("results"));
            JSONArray types = ( (JSONObject)ja.get(0)).getJSONArray("types");
             for (int i = 0; i < types.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
               //here I stop  
            }

and this is the JSON response

"results" : [
      {
         "formatted_address" : "529 Kent Street, Sydney NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8750460,
               "lng" : 151.2052720
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "827f1ac561d72ec25897df088199315f7cbbc8ed",
         "name" : "Tetsuya's",
         "rating" : 4.30,
         "reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg",
         "types" : [ "restaurant", "food", "establishment" ]
      },

so would you please help me figure out a solution to this problem? Thank you

Was it helpful?

Solution

 String testData = "{\"results\" : [ "
            + "{\"formatted_address\" : \"529 Kent Street, Sydney NSW, Australia\", "
            + "\"geometry\" : {"
            + "\"location\" : {"
            + "\"lat\" : -33.8750460,"
            + "\"lng\" : 151.2052720}"
            + "},"
            + "\"icon\" : \"http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png\","
            + "\"id\" : \"827f1ac561d72ec25897df088199315f7cbbc8ed\", "
            + "\"name\" : \"Tetsuya's\","
            + "\"rating\" : 4.30,"
            + "\"reference\" : \"CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg\", "
            + "\"types\" : [ \"restaurant\", \"food\", \"establishment\" ] "
            + "}] }";
    JSONObject predictions;
    try {
        predictions = new JSONObject(testData);
        JSONArray ja = (JSONArray) predictions.get("results");
        JSONArray types = (JSONArray) ((JSONObject) ja.get(0)).get("types");
        String result = types.toString();
        result = result.substring(1, result.length() - 1);
        String[] allTypes = result.split(",");
        for(int i = 0; i < allTypes.length;i++) {
            String type = allTypes[i];
            //save your type
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

OTHER TIPS

//Supposing that you get the complete response as one JSONObject called 'res' as follows:

JSONObject res = new JSONObject();
try {
    res = new JSONObject(stringBuilder.toString()); // stringBuilder is string which you append everthing when you are reading the stream
} catch (JSONException e) {
    e.printStackTrace();
}

You can extract the values corresponding to 'types' from the resulting 'res' JSON Object as follows:

JSONObject type;
String type_string;
try {
    type = ret.getJSONArray("results").getJSONObject(0);
    type_string = type.getString("types");
    Log.d("test", "formattted address:" + location_string);

} catch (JSONException e1) {
    e1.printStackTrace();

}

In 'type_string' you should have the value string which is in front of 'types', i.e something like:

["restaurant", "food", "establishment"]

Then you can use .Split() function to split the String using "," as the delimiter. Something like :

String[] str_array = type_string.split(",");
String stringa = str_array[0]; 
String stringb = str_array[1];
String stringC = str_array[2];

The above str_array will store the type strings.

P.S: I've not run this code. I just wanted to give you an idea how to approach your problem. You might need to tweak few things to fit into your existing code. Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top