0

我在这里遇到问题。在我的android应用程序中,我有这个地方的位置,所以我相对想得到这个地方所属的类型。所以我找到了解决方案,"types" : [ "restaurant", "food", "establishment" ]但问题是我不知道如何从这个 JSONObject 中提取每种类型。这是一个类似于我想要的东西的例子。

 URL googlePlaces = new URL(
                        "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +URLEncoder.encode(args[0], "UTF-8")+"&sensor=true&key=key");
 URLConnection tc = googlePlaces.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            StringBuffer sb = new StringBuffer();
            //take Google's legible JSON and turn it into one big string.
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
                JSONObject predictions = new JSONObject(sb.toString()); 
 JSONArray ja = new JSONArray(predictions.getString("predictions"));
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                     predictionsArr.add(jo.getString("description"));
                }
            }
4

0 回答 0