我如何从JSON GoogleDirection获取TagName

时间:2018-07-02 14:15:14

标签: java android

我尝试计算两点之间的距离和行进时间,我使用下面的方法,问题是我不知道从JSON文件获取TagName:Distance TEXT和Duration TEXT,以便像String那样传递它到TextView

public void test2() throws JSONException {

    JSONObject locationJsonObject = new JSONObject();
    locationJsonObject.put("origin", "54.406505,18.67708");
    locationJsonObject.put("destination", "54.446251,18.570993");
    LatlngCalc(locationJsonObject);
}
private void LatlngCalc(JSONObject locationJsonObject) throws JSONException {

    RequestQueue queue = Volley.newRequestQueue(ge_Traker_maps.this);
    String url = "http://maps.googleapis.com/maps/api/distancematrix/" +
            "json?origins=" + locationJsonObject.getString("origin") + "&destinations=" + locationJsonObject.getString("destination") + "&mode=driving&" +
            "language=en-EN&sensor=false";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    tv4.setText("Response is: " + response.substring(0, 500));

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            tv4.setText("That didn't work!");
        }
    });
    queue.add(stringRequest);
}

JSON文件示例:

"rows":[
{
"elements":[
{
"distance":{
"text": "15.2 Km"
"value": 15167
},
"duration":{
"text": "24 mins"
"value": 1466
},
"status": "ok"

2 个答案:

答案 0 :(得分:0)

    JSONObject distance = object.getJSONObject("distance");

    if (distance.has("distance") && !distance.getString("distance").equals("")) {

     String text = distance.getString("text");
     String value = distance.getString("value");

    }
JSONObject duration = object.getJSONObject("duration");
    if (duration.has("duration") && !duration.getString("duration").equals("")) {

     String text = duration.getString("text");
     String value = duration.getString("value");

    }

答案 1 :(得分:0)

以下代码假定您使用com.googlecode.json-simple:json-simple库。它还在您的问题http://maps.googleapis.com/maps/api/distancematrix/json?origins=54.406505,18.67708&destinations=54.446251,18.570993&mode=driving&language=en-EN&sensor=false

中使用url
JSONParser jsonParser = new JSONParser();

try {
  Object json = jsonParser.parse(jsonString);

  JSONObject jsonObject = (JSONObject) json;

  JSONArray rowsArray = (JSONArray) jsonObject.get("rows");

  for (Object aRow : rowsArray) {
    JSONObject aRowObject = (JSONObject) aRow;

    JSONArray elementArray = (JSONArray) aRowObject.get("elements");

    for (Object anElement : elementArray) {
      JSONObject distanceObj = (JSONObject) ((JSONObject) anElement).get("distance");

      String text = (String) distanceObj.get("text");
      long value = Long.valueOf((String) distanceObj.get("value"));

      System.out.println(text + " " + value);
    }
  }
} catch (ParseException e) {
  e.printStackTrace();
}

希望这会有所帮助。