【问题标题】:Why can't I get a specific object from this JSON document on Android?为什么我在 Android 上无法从此 JSON 文档中获取特定对象?
【发布时间】:2012-11-08 21:54:44
【问题描述】:

我想从JSONObject 获取折线,但以下代码不起作用。

JSONObject poly = steps.getJSONObject("polyline"); 

我得到这个错误:

org.json.JSONException: 折线没有值

这段代码运行良好

JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONObject endLoc = steps.getJSONObject("end_location");
JSONObject duration = steps.getJSONObject("duration");

传入的JSON文档是

{
  "duration": {
    "value": 363,
    "text": "6 mins"
  },
  "distance": {
    "value": 506,
    "text": "0.5 km"
  },
  "end_location": {
    "lng": 11.48949,
    "lat": 53.52159
  },
  "start_address": "PCH30, 19079 Banzkow, Germany",
  "end_address": "PCH30, 19079 Banzkow, Germany",
  "start_location": {
    "lng": 11.48189,
    "lat": 53.52114
  },
  "via_waypoint": [

  ],
  "steps": [
    {
      "html_instructions": "Head <b>east<\/b> on <b>PCH30<\/b><div style=\"font-size:0.9em\">Destination will be on the left<\/div>",
      "duration": {
        "value": 363,
        "text": "6 mins"
      },
      "distance": {
        "value": 506,
        "text": "0.5 km"
      },
      "end_location": {
        "lng": 11.48949,
        "lat": 53.52159
      },
      "polyline": {
        "points": "cjteIypaeAGqBKaEGaBC{@A_AM{EMwGMwFIsDC}@"
      },
      "travel_mode": "WALKING",
      "start_location": {
        "lng": 11.48189,
        "lat": 53.52114
      }
    }
  ]
}

以及获取JSONObject的完整代码

StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");// from
urlString.append(Double.toString((double) gpAlt.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) gpAlt.getLongitudeE6() / 1E6));
urlString.append("&destination=");// to
urlString.append(Double.toString((double) gpNeu.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) gpNeu.getLongitudeE6() / 1E6));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx", "URL=" + urlString.toString());

urlConnection = null;
URL url = null;

url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();

InputStream inStream = urlConnection.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));

String temp, response = "";
while ((temp = bReader.readLine()) != null) 
{
    response += temp;
}

bReader.close();
inStream.close();
urlConnection.disconnect();

JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
JSONArray array = object.getJSONArray("routes");
JSONObject routes = array.getJSONObject(0);
String summary = routes.getString("summary");
JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONObject poly = steps.getJSONObject("polyline");  

【问题讨论】:

  • 我认为您在当前代码中会遇到其他问题,因为您在此处共享的代码对于我来说是完美的,可以从 json 字符串中获取折线值。

标签: android json polyline


【解决方案1】:

除非您显示的 JSON 不正确,否则您需要做的是

JSONArray steps = routes.getJSONArray("steps");
JSONObject obj = steps.getJSONObject(0);
JSONObject poly = obj.getJSONObject("polyline");  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2016-09-16
    • 2017-07-03
    相关资源
    最近更新 更多