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

eam*_*813 5 java android json google-maps

我是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
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

{
   "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"
}
Run Code Online (Sandbox Code Playgroud)

我试过了:

1.

JSONArray jsonArray = jObject.getJSONArray("rows");
JSONArray routes = jsonArray.getJSONArray(0);
stringBuilder.append(routes.getJSONObject(0).getString("text"));
Run Code Online (Sandbox Code Playgroud)

2.

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

3.

JSONArray jsonArray = jObject.getJSONArray("elements");                    stringBuilder.append(routes.getJSONObject(0).getString("text"));
Run Code Online (Sandbox Code Playgroud)

我尝试了更多,但在我看来,他们应该工作.对我而言,行似乎是一个数组,元素也是一个数组.因此,我需要从原始JSONObject中获取行,然后从行数组中获取元素数组,然后从该数组中获取距离对象,然后最终获取文本值并将其添加到字符串构建器I之前创建的.我错了吗?预先感谢您的任何帮助.

anu*_*v16 8

这对我有用

//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();
Run Code Online (Sandbox Code Playgroud)

[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();
Run Code Online (Sandbox Code Playgroud)