来自Google商家信息的JSON

时间:2013-04-22 01:23:04

标签: android json google-places-api google-places

在这里,我想使用Google Places API获取特定地点的类型,但我显然遇到了如何从JSON格式获取所需“类型”值的巨大问题。

这是我试图做的事情

 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  
            }

这是JSON响应

"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" ]
      },

那么请你帮我找出解决这个问题的方法吗?谢谢

2 个答案:

答案 0 :(得分:1)

 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();
    }

答案 1 :(得分:0)

//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();
}

您可以从生成的'res'JSON对象中提取与'types'对应的值,如下所示:

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();

}

在'type_string'中你应该有'types'前面的值字符串,即 类似的东西:

  

[“restaurant”,“food”,“establishment”]

然后你可以使用.Split()函数来分割字符串,使用“,”作为分隔符。类似的东西:

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

上面的str_array将存储类型字符串。

P.S:我没有运行此代码。我只想告诉你如何处理你的问题。您可能需要调整一些内容以适应现有代码。希望能帮助到你。