首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android数据检索

android数据检索
EN

Stack Overflow用户
提问于 2016-03-08 05:49:11
回答 5查看 305关注 0票数 1

我对Json很陌生。我想用json检索两个地方之间的距离。我想从“腿”数组中的“距离”对象中获取“文本”(距离b/w两个位置),而后者又位于“路由”数组中。链接(http://maps.googleapis.com/maps/api/directions/json?origin=Adoor&destination=Thiruvananthapuram%20Zoo&sensor=false)

Java代码:

代码语言:javascript
复制
 String uri="http://maps.googleapis.com/maps/api/directions/json?origin="+destination+"&destination="+tour_place+"&sensor=false";
            queue= Volley.newRequestQueue(getApplicationContext());
            JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.GET, uri, (String) null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                         JSONArray array=response.getJSONArray("legs");
                 distances.add(array.getJSONObject(0).getJSONObject("distance").getDouble("text"));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error){

                }
            });
                queue.add(objectRequest);
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-03-08 06:34:24

好的,现在将我做过的代码发布出来。试试这个。

代码语言:javascript
复制
try {
            JSONObject json = new JSONObject(jStr);
            JSONArray jaRoutes = json.getJSONArray("routes");
            JSONArray jaLegs = jaRoutes.getJSONObject(0).getJSONArray("legs");
            JSONObject joDistance = jaLegs.getJSONObject(0).getJSONObject("distance");
            String text = joDistance.getString("text");
            Toast.makeText(this, text, Toast.LENGTH_SHORT);
        } catch (JSONException e) {
            Log.d("SAMPLE", e.toString());
        }

如果有用的话请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2016-03-08 05:59:47

尝尝这个

代码语言:javascript
复制
try{

JSONObject object = new JSONObject(thewholething.toString());
JSONArray routes =object.getJSONArray("routes");
JSONArray legs=routes.getJSONObject(0).getJSONArray("legs")
JSONObject distance =legs.getJSONObject(0).getJSONObject("distance");
String text=distance.getString("text");

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

现在,字符串文本包含您的值。

票数 0
EN

Stack Overflow用户

发布于 2016-03-08 06:09:34

从响应中,首先解析路由数组。

代码语言:javascript
复制
JsonArray routes = response.getJsonArray("routes");

从这里,您可以得到如下所示的腿数组。

代码语言:javascript
复制
JsonArray legs = routes.getJsonArray("legs");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35859944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档