2014-02-20 59 views
1

我想解析从我的android程序中谷歌方向api的JSON响应。这是请求链接。我在这里跳过JSON响应,因为它太长,只需点击链接就会显示它。解析从谷歌方向api的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

回答

1

正如你不太熟悉,我建议使用GSON解析JSON,尤其对于复杂的反应。这种情况在你的情况下会更好。

我用GSON从谷歌地图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/

命名差异(根据在web服务中的变量),可以使用注释等 @SerializedName。 (所以不需要使用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,而不是为(;;)在以往任何时候可能的。

检查这些:
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

+0

感谢您向我介绍gson。它和android自己的json类一样高效吗? – Raiyan

+0

它的效率很高,除此之外,代码行数量减少了很多,代码变得不太容易出错。而当数据映射到对象时,它更容易在相同的情况下操作。 – user2450263

+0

太好了,再次感谢。 – Raiyan

0

for循环永远不会设置了step值 - 你在你的for顶部缺少一行

step = steps.getJSONObject(i); 

循环?

+0

你对,当我将代码从IDE复制到网站时,我错过了该行。帖子被编辑。 – Raiyan

0

试试这个..

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"); 
     } 
    } 
+0

对不起,我从某种程度上错过了将代码从IDE复制到网站的一行。我编辑了这篇文章。谢谢你的时间。 – Raiyan