2014-02-20 60 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