0

我想从 a 获取折线,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");  
4

1 回答 1

0

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

JSONArray steps = routes.getJSONArray("steps");
JSONObject obj = steps.getJSONObject(0);
JSONObject poly = obj.getJSONObject("polyline");  
于 2012-12-19T19:15:16.823 回答