我对Json很陌生。我想用json检索两个地方之间的距离。我想从“腿”数组中的“距离”对象中获取“文本”(距离b/w两个位置),而后者又位于“路由”数组中。链接(http://maps.googleapis.com/maps/api/directions/json?origin=Adoor&destination=Thiruvananthapuram%20Zoo&sensor=false)
Java代码:
String uri="http://maps.googleapis.com/maps/api/directions/json?origin="+destination+"&destination="+tour_place+"&sensor=false";
queue= Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.GET, uri, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array=response.getJSONArray("legs");
distances.add(array.getJSONObject(0).getJSONObject("distance").getDouble("text"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error){
}
});
queue.add(objectRequest);发布于 2016-03-08 06:34:24
好的,现在将我做过的代码发布出来。试试这个。
try {
JSONObject json = new JSONObject(jStr);
JSONArray jaRoutes = json.getJSONArray("routes");
JSONArray jaLegs = jaRoutes.getJSONObject(0).getJSONArray("legs");
JSONObject joDistance = jaLegs.getJSONObject(0).getJSONObject("distance");
String text = joDistance.getString("text");
Toast.makeText(this, text, Toast.LENGTH_SHORT);
} catch (JSONException e) {
Log.d("SAMPLE", e.toString());
}如果有用的话请告诉我。
发布于 2016-03-08 05:59:47
尝尝这个
try{
JSONObject object = new JSONObject(thewholething.toString());
JSONArray routes =object.getJSONArray("routes");
JSONArray legs=routes.getJSONObject(0).getJSONArray("legs")
JSONObject distance =legs.getJSONObject(0).getJSONObject("distance");
String text=distance.getString("text");
}
catch (JSONException e) {
e.printStackTrace();
}现在,字符串文本包含您的值。
发布于 2016-03-08 06:09:34
从响应中,首先解析路由数组。
JsonArray routes = response.getJsonArray("routes");从这里,您可以得到如下所示的腿数组。
JsonArray legs = routes.getJsonArray("legs");https://stackoverflow.com/questions/35859944
复制相似问题