-2

我是否需要从下面的 url 解析 json 并将距离和持续时间的数据放在列表视图中,可能吗?

http://maps.googleapis.com/maps/api/directions/json?origin=-25.3641,-49.2857&destination=-25.3928,-49.2728®ion=en&sensor=false

4

1 回答 1

0

你的问题不是很清楚。你需要先解析json。您可以将解析后的数据放入ArrayListof 中Bundle。执行以下操作:

String response = getResponseFromGoogleMaps(); //this function will fetch the response. write it in your way
ArrayList<Bundle> list = new ArrayList<Bundle>();
try {
        JSONObject json = new JSONObject(response);
        JSONArray routes = json.getJSONArray("route");
        JSONArray legs = routes.getJSONArray(0);
        JSONArray steps = legs.getJSONArray(0);
        for(int i=0;i<steps.length();i++) {
            JSONObject singleStep = steps.getJSONObject(i);
            JSONObject duration = singleStep.getJSONObject("duration");
            Bundle dur = new Bundle();
            dur.putString("text", duration.getString("text"));
            dur.putString("value", duration.getString("value"));
            JSONObject distance = singleStep.getJSONObject("distance");
            Bundle dis = new Bundle();
            dis.putString("text", distance.getString("text"));
            dis.putString("value", distance.getString("value"));
            Bundle data = new Bundle();
            data.putBundle("duration", dur);
            data.putBundle("distance", dis);
            list.add(data);
        }
} catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
}

然后创建您自己的列表适配器(应该从 扩展BaseAdapter)来处理ArrayList<Bundle>. 你完成了!

于 2012-04-26T19:46:33.257 回答