从谷歌API的方向解析JSON响应 [英] Parsing json response from google direction api

查看:312
本文介绍了从谷歌API的方向解析JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析来自谷歌的方向API在我的Andr​​oid程序JSON响应。这是请求链接波纹管。我在这里跳过JSON响应,因为它太长,只需点击链接可证明这一点。

I'm trying to parse the JSON response from google direction api in my android program. This is the request link bellow. I'm skipping the JSON response here because its too long and simply clicking on the link will show it.

<一个href=\"http://maps.googleapis.com/maps/api/directions/json?origin=Windsor&destination=Leamington&sensor=false&avoid=highways&mode=walking\" rel=\"nofollow\">http://maps.googleapis.com/maps/api/directions/json?origin=Windsor&destination=Leamington&sensor=false&avoid=highways&mode=walking

我的code解析响应:

My code for parsing the response :

try {
    JSONObject responseObject = (JSONObject) new JSONTokener(responseString).nextValue();
    this.responseString = responseObject.getString("status") ;
    JSONArray routesArray = responseObject.getJSONArray("routes");
    JSONObject route = routesArray.getJSONObject(0);
    JSONArray legs ;
    JSONObject leg ;
    JSONArray steps ;
    JSONObject dist;
    Integer distance ;
    if(route.has("legs")) {
        legs = route.getJSONArray("legs");
        leg = legs.getJSONObject(0) ;
        steps = leg.getJSONArray("steps"); // EDIT : I had somehow missed this line before when copying the code from IDE to the website
        int nsteps = steps.length() ;
        for(int i=0;i<nsteps;i++) {
        JSONObject step = steps.getJSONObject(i);
            if(step.has("distance")) {
                dist = (JSONObject) step.get("distance");// throws exception
                //I would like to take the distance value and do something with it
                //if(dist.has("value"))
                //  distance = (Integer) dist.get("value") ;
            }
        }
    }
    else
        this.responseString = "not found" ;
} catch ( Exception e) {
    e.printStackTrace() ;
}

这抛出异常(再次跳过,因为JSON响应太大堆栈跟踪显示了整个响应字符串):

This throws an exception (again skipping because the JSON response is too big. The stack trace shows the entire response string) :

org.json.JSONException: Expected ':' after polyline at character 13837 of {
    "routes" : [
       {
          "bounds" : {
             "northeast" : { ....

我已经使用 getJSONObject 函数代替 GET 试过,但我得到了同样的异常。

I have tried using the getJSONObject function instead of get, but I get the same exception.

dist = step.getJSONObject("distance");

任何人都可以请指出我丢失在这里帮助我吗?我不是很熟悉解析的源代码JSON在Android还,所以它很可能是我在做一个愚蠢的错误的地方。谢谢你。

Can anyone please help me by pointing out what am I missing here ? I'm not very familiar with parsin JSON on android yet, so it's quite likely that I'm making a silly mistake somewhere. Thanks.

这个网站的其他类似的职位,但并不完全一样:<一href=\"http://stackoverflow.com/questions/7237290/json-parsing-of-google-maps-api-in-android-app\">JSON在Android应用程序谷歌地图API的解析

Another similar post on this site, but not quite the same : JSON parsing of Google Maps API in Android App

推荐答案

由于你不是那么熟悉,我建议使用GSON,尤其对于复杂的反应解析JSON。该案件将在你的情况适合更好。

As you aren't so familiar, i'd suggest parsing JSON using Gson, esp for complex responses. The case would suit better in your case.

我用GSON从谷歌地图API,API的地方和更多...

I have used Gson for parsing responses from Google Maps API, Places API and more...

您可以用GSON解析,地图数据和模型类会更好。例如:

You may be better off using Gson to parse, map data and your model classes. Eg.:

    Gson gson = new Gson();
    ModelClass modelClass= new ModelClass(); //or ArrayList<ModelClass>
    modelClass= gson.fromJson(responseContent,ModelClass.class); 
//where responseContent is your jsonString
    Log.i("Web service response", ""+modelClass.toString());

的https://$c$c.google.com/p/google -gson /

有关命名差异(根据在web服务中的变量),可以使用像注解
@SerializedName。 (所以没必要使用序列化)

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName. (So no need to use Serializable)

您可以从模型类使用for-each循环和验证或操作的数据。

You can use a for-each loop and verify or operate the data from the model classes.

for(Modelclass object: modelClass.getList()) {
}

http://docs.oracle.com/ JavaSE的/ 1.5.0 /文档/引导/语言/ foreach.html

如何在Java for each循环工作?

此外,使用的for-each,而不是为(;;)在以往可能。

Also, use a for-each instead of for(;;) where ever possible.

检查这些:结果
从谷歌的地方与解析数据GSON 结果
HTTP://www.java$c $ cgeeks.com / 2011/01 / Android的JSON的解析 - GSON-tutorial.html 结果
如何分析谷歌的地方JSON

Check these:
Parsing data with gson from google places
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
How to parse google places json

我的工作相同的API,现在,认为这也将有助于为模型类:结果
<一href=\"http://stackoverflow.com/questions/19290593/displaying-multiple-routes-using-directions-api-in-android\">Displaying使用地图API在Android中多个路由

I am working on the same API right now, thought this would also help, for the model classes:
Displaying multiple routes using Directions API in Android

这篇关于从谷歌API的方向解析JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆