簡體   English   中英

解析來自google的JSON映射中的DistanceMatrix api

[英]Parsing JSON from the google maps DistanceMatrix api in android

我是Android開發的新手,也是JSON的新手。 我正在使用谷歌地圖距離矩陣api。 我相信,JSON下載到JSONObject中是正確的。(我從另一篇文章中偷了代碼)。 但是我似乎無法正確解析JSON。 我已經在這工作了幾天而且完全難過了。 我在下面打電話給谷歌

https://maps.googleapis.com/maps/api/distancematrix/json?origins=1600%20pennsylvania%20avenue&destinations=1500%20college%20street&mode=driving&units=imperial

輸出是這樣的:

{
   "destination_addresses" : [ "1500 College Street, Beaumont, TX 77701, USA" ],
   "origin_addresses" : [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,306 mi",
                  "value" : 2102536
               },
               "duration" : {
                  "text" : "18 hours 48 mins",
                  "value" : 67684
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

我試過了:

1。

JSONArray jsonArray = jObject.getJSONArray("rows");
JSONArray routes = jsonArray.getJSONArray(0);
stringBuilder.append(routes.getJSONObject(0).getString("text"));

2。

JSONArray jsonArray = jObject.getJSONArray("rows");
JSONObject routes = jsonArray.getJSONObject(0);
stringBuilder.append(routes.getJSONObject("distance").getString("text"));

3。

JSONArray jsonArray = jObject.getJSONArray("elements");                    stringBuilder.append(routes.getJSONObject(0).getString("text"));

我嘗試了更多,但在我看來,他們應該工作。 對我而言,行似乎是一個數組,元素也是一個數組。 因此,我需要從原始JSONObject中獲取行,然后從行數組中獲取元素數組,然后從該數組中獲取距離對象,然后最終獲取文本值並將其添加到字符串構建器I之前創建的。 我錯了嗎? 預先感謝您的任何幫助。

這對我有用

//httpResponse is the output of google api
JSONObject jsonRespRouteDistance = new JSONObject(httpResponse)
                                        .getJSONArray("rows")
                                        .getJSONObject(0)
                                        .getJSONArray ("elements")
                                        .getJSONObject(0)
                                        .getJSONObject("distance");

String distance = jsonRespRouteDistance.get("text").toString();

/* 
* For distance, below is only partial solution as the 
* output to string destination_addr will contain square brackets [] and double codes ""
* Eg. [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ]
* 
*/
String destination_addr = new JSONObject(httpResponse)
                            .get("destination_addresses")
                            .toString();

[UPDATE]

假設我們只有一個目的地址而不是多個。 一個小的字符串操作得到我們干凈的字符串字符串沒有代碼“和括號[]

StringBuilder stringBuilderDestinationAddr = new StringBuilder();

for (int i = 0; i < destination_addr.length(); i++)
    if (destination_addr.charAt(i) != '[' && destination_addr.charAt(i) != ']' && destination_addr.charAt(i) != '"')
         stringBuilderDestinationAddr.append(pickup_addr.destination_addr (i));

String strCleanDestination = stringBuilderDestinationAddr.toString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM