Android App中Google Maps API的JSON解析 [英] JSON parsing of Google Maps API in Android App

查看:17
本文介绍了Android App中Google Maps API的JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用谷歌地图 API 来获取方向时间.我希望创建一个 url,获取 JSON 响应,然后检查该响应的旅行持续时间.创建 JSON 对象后,我无法导航它.对我来说,这表明我在获取响应或导航 JSON 对象时搞砸了.如果您能看一看我从网上教程拼凑起来的零碎代码,我将不胜感激.

I'm trying to use the google maps API to fetch direction times. I'm hoping to create a url, get the JSON response, and then examine that response for travel duration. After I create the JSON object, I have trouble navigating it. To me, this indicates that I have either messed up getting the response or navigating the JSON object. I'd appreciate it if you could peek at the bits and pieces of code I have stitched together from tutorials around the web.

此代码旨在获得响应.它被 try/catch 包围,并且没有触发任何错误.

This code is intended to get the response. It's surrounded by a try/catch and it hasn't triggered any errors.

      String stringUrl = <URL GOES HERE>;

      URL url = new URL(stringUrl);
      HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
      if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
      {
          BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
          String strLine = null;
          while ((strLine = input.readLine()) != null)
          {
              response.append(strLine);
          }
          input.close();
      }
      String jsonOutput = response.toString();

此代码旨在获取该输出并将其解析为最终字符串持续时间,其灵感来自 this stackoverflow answer 用于类似问题.

This code is intended to take that output and parse it into the final string, duration, as inspired by this stackoverflow answer for a similar question.

        JSONObject jsonObject = new JSONObject(jsonOutput);
        JSONObject routeObject = jsonObject.getJSONObject("routes");
        JSONObject legsObject = routeObject.getJSONObject("legs");
        JSONObject durationObject = legsObject.getJSONObject("duration"); 
        String duration = durationObject.getString("text");

我在第二个块的第二行捕捉到一个 JSON 异常.任何人都可以帮助解决这个问题吗?或者提出一种更简单的方法来获取相同的数据?

I'm catching a JSON exception on the second line of the second block. Can anyone help to fix this? Or perhaps suggest a simpler way to get the same data?

感谢这里的第一个有用答案(来自 aromero),后半部分现在看起来像:

    JSONObject jsonObject = new JSONObject(responseText);
    JSONArray routeObject = jsonObject.getJSONArray("routes");
    JSONArray legsObject = routeObject.getJSONArray(2); ***error***
    JSONObject durationObject = legsObject.getJSONObject(1);
        String duration = durationObject.getString("text");

但它仍然抛出一个 JSON 异常,只是现在它在第三行之后.我敢肯定这很容易解决,但我真的很感激一些帮助.

But it is still throwing a JSON exception, only now it's after the third line. I'm sure this is embarrassingly easy to fix, but I would really appreciate some help.

示例 JSON 文件的相关部分如下所示:

The relevant part of an example JSON file is shown below:

{
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 34.092810,
               "lng" : -118.328860
            },
            "southwest" : {
               "lat" : 33.995590,
               "lng" : -118.446040
            }
         },
         "copyrights" : "Map data ©2011 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "12.9 mi",
                  "value" : 20807
               },
               "duration" : {
                  "text" : "27 mins",
                  "value" : 1619
               },

推荐答案

"routes" 是一个数组,使用 getJSONObject 代替 getJSONArray

"routes" is an array, instead of using getJSONObject, use getJSONArray

legs"也是一个数组.

"legs" is an array also.

JSONObject jsonObject = new JSONObject(responseText);

// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
// Take all legs from the route
JSONArray legs = route.getJSONArray("legs");
// Grab first leg
JSONObject leg = legs.getJSONObject(0);

JSONObject durationObject = leg.getJSONObject("duration");
String duration = durationObject.getString("text");

这篇关于Android App中Google Maps API的JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆