为什么我不能从Android上的这个JSON文档中获取特定对象?

时间:2012-11-20 18:37:02

标签: android json polyline

我想从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");  

1 个答案:

答案 0 :(得分:0)

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

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