【问题标题】:Parsing json response from google direction api解析来自谷歌方向api的json响应
【发布时间】:2014-02-20 03:59:25
【问题描述】:

我正在尝试在我的 android 程序中解析来自 google direction api 的 JSON 响应。这是下面的请求链接。我在这里跳过 JSON 响应,因为它太长,只需单击链接就会显示它。

http://maps.googleapis.com/maps/api/directions/json?origin=Windsor&destination=Leamington&sensor=false&avoid=highways&mode=walking

我解析响应的代码:

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 响应太大。堆栈跟踪显示整个响应字符串):

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

我尝试使用getJSONObject 函数而不是get,但我得到了同样的异常。

dist = step.getJSONObject("distance");

任何人都可以通过指出我在这里缺少什么来帮助我吗?我对 android 上的 parsin JSON 还不是很熟悉,所以我很可能在某个地方犯了一个愚蠢的错误。谢谢。

此站点上的另一个类似帖子,但不完全相同:JSON parsing of Google Maps API in Android App

【问题讨论】:

    标签: android json google-maps parsing response


    【解决方案1】:

    由于您不太熟悉,我建议使用 Gson 解析 JSON,尤其是复杂响应。这个案子更适合你的案子。

    我使用 Gson 来解析来自 Google Maps API、Places API 等的响应...

    使用 Gson 解析、映射数据和模型类可能会更好。例如:

        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://code.google.com/p/google-gson/

    对于命名差异(根据webservice中的变量),可以使用如下注解 @序列化名称。 (所以不需要使用Serializable)

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

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

    http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

    How does the Java 'for each' loop work?

    此外,尽可能使用 for-each 代替 for(;;)。

    检查这些:
    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,认为这对模型类也有帮助:
    Displaying multiple routes using Directions API in Android

    【讨论】:

    • 感谢您向我介绍 gson。和 androids 自己的 json 类一样高效吗?
    • 它高效,除此之外,代码行数减少了相当多,代码变得不易出错。并且当数据映射到对象时,它更容易操作。
    【解决方案2】:

    试试这个..

        steps = leg.getJSONArray("steps");
        int nsteps = steps.length() ;
        for(int i=0;i<nsteps;i++) {
            if(steps.has("distance")) {
                JSONObject new_onj = steps.getJSONObject(i);
                dist = new_onj.getJSONObject("distance");       
                if(dist.has("value"))
                   distance = (Integer) dist.get("value");
            }
        }
    

    【讨论】:

    • 很抱歉,我在将代码从 IDE 复制到网站时不知何故遗漏了一行。我已经编辑了帖子。感谢您的宝贵时间。
    【解决方案3】:

    您的 for 循环从未为 step 设置值 - 您是否漏掉了一行

    step = steps.getJSONObject(i);
    

    for 循环的顶部?

    【讨论】:

    • 你说得对,当我将代码从 IDE 复制到网站时,我错过了这一行。帖子已编辑。
    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 2014-03-26
    • 2011-09-23
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2013-03-16
    相关资源
    最近更新 更多