谷歌地图API中的Andr​​oid应用程序JSON解析 [英] JSON parsing of Google Maps API in Android App

查看:94
本文介绍了谷歌地图API中的Andr​​oid应用程序JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用谷歌地图API来获取方向的时间。我希望创建一个URL,得到JSON响应,然后检查行程持续时间响应。当我创建JSON对象,我有麻烦浏览它。对我来说,这表明我要么搞砸得到响应或导航JSON对象。我倒是AP preciate如果你能偷看我从各地的网络教程缝合在一起位和code片。

这code的目的是获得响应。它的周围有一个try / catch,也没有引发任何错误。

 字符串stringUrl =< URL GOES HERE取代;

      网址URL =新的URL(stringUrl);
      HttpURLConnection的httpconn =(HttpURLConnection类)url.openConnection();
      如果(httpconn.getResponse code()== HttpURLConnection.HTTP_OK)
      {
          BufferedReader中输入=新的BufferedReader(新的InputStreamReader(httpconn.getInputStream()),8192);
          字符串strLine中= NULL;
          而((strLine中= input.readLine())!= NULL)
          {
              response.append(strLine中);
          }
          input.close();
      }
      串jsonOutput = response.toString();
 

这code旨在采取输出和解析成最后一个字符串,持续时间,灵感来自<一href="http://stackoverflow.com/questions/2951007/android-get-duration-from-maps-google-com-directions/2953077#2953077"标题=这个计算器的答案>这个计算器回答了类似的问题。

 的JSONObject的JSONObject =新的JSONObject(jsonOutput);
        JSONObject的routeObject = jsonObject.getJSONObject(路);
        JSONObject的legsObject = routeObject.getJSONObject(腿);
        JSONObject的durationObject = legsObject.getJSONObject(期限);
        字符串长度= durationObject.getString(文字);
 

我赶上第二块的第二行的JSON异常。任何人都可以帮助解决这一问题?或者提出一个简单的方法来获得相同的数据?

编辑:多亏了这里的第一个有用的答案(来自aromero),后半部分现在看起来像:

 的JSONObject的JSONObject =新的JSONObject(responseText的);
    JSONArray routeObject = jsonObject.getJSONArray(路);
    JSONArray legsObject = routeObject.getJSONArray(2); ***错误***
    的JSONObject durationObject = legsObject.getJSONObject(1);
        字符串长度= durationObject.getString(文字);
 

但它仍然抛出一个JSON例外,只是现在它的第三个行之后。我敢肯定,这是令人尴尬容易解决,但我真的AP preciate一些帮助。

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

  {
   路线:
      {
         界限:{
            东北 : {
               纬度:34.092810,
               液化天然气:-118.328860
            },
            西南 : {
               纬度:33.995590,
               液化天然气:-118.446040
            }
         },
         版权:地图数据©2011谷歌,
         腿:
            {
               距离 : {
                  文:12.9英里,
                  值:20807
               },
               持续时间:{
                  文:27分钟,
                  值:1619
               },
 

解决方案

路线而不是使用数组, getJSONObject ,使用 getJSONArray

腿是一个数组也。

 的JSONObject的JSONObject =新的JSONObject(responseText的);

// routesArray包含所有路线
JSONArray routesArray = jsonObject.getJSONArray(路);
//取得第一路线
JSONObject的路线= routesArray.getJSONObject(0);
//采取一切腿路线
JSONArray腿= route.getJSONArray(腿);
//取得首回合
的JSONObject腿= legs.getJSONObject(0);

JSONObject的durationObject = leg.getJSONObject(期限);
字符串长度= durationObject.getString(文字);
 

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.

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 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");

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?

EDIT: thanks to the first helpful answer here (from aromero), the latter half now looks like:

    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");

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.

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" is an array, instead of using getJSONObject, use getJSONArray

"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");

这篇关于谷歌地图API中的Andr​​oid应用程序JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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