0

在这里,我想使用 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" ]
      },

所以你能帮我找出解决这个问题的方法吗?谢谢

4

2 回答 2

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();
    }
于 2013-04-22T04:56:19.927 回答
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'前面的值字符串,例如:

[“餐厅”、“食物”、“机构”]

然后,您可以使用 .Split() 函数使用“,”作为分隔符来拆分字符串。就像是 :

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

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

PS:我没有运行这个代码。我只是想告诉你如何解决你的问题。您可能需要调整一些东西以适应您现有的代码。希望能帮助到你。

于 2013-04-22T02:18:48.050 回答