我是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);
答案 0 :(得分:1)
好的,现在发布我做的代码。试试这个。
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());
}
让我知道它是否有效。
答案 1 :(得分:0)
试试这个
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();
}
现在String文本包含您的值。
答案 2 :(得分:0)
从您的回复中,您首先解析您的路线数组。
JsonArray routes = response.getJsonArray("routes");
从这里开始,您可以获得如下所示的腿部阵列。
JsonArray legs = routes.getJsonArray("legs");
答案 3 :(得分:0)
试试这个,
List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
JSONArray jRoutes = null;
JSONArray jLegs = null;
JSONArray jSteps = null;
JSONObject jDistance = null;
JSONObject jDuration = null;
try {
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for(int i=0;i<jRoutes.length();i++){
jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>();
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++){
/** Getting distance from the json data */
jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");
HashMap<String, String> hmDistance = new HashMap<String, String>();
hmDistance.put("distance", jDistance.getString("text"));
/** Getting duration from the json data */
jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
HashMap<String, String> hmDuration = new HashMap<String, String>();
hmDuration.put("duration", jDuration.getString("text"));
/** Adding distance object to the path */
path.add(hmDistance);
/** Adding duration object to the path */
path.add(hmDuration);
jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
/** Traversing all steps */
for(int k=0;k<jSteps.length();k++){
String polyline = "";
polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
List<LatLng> list = decodePoly(polyline);
/** Traversing all points */
for(int l=0;l<list.size();l++){
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) );
hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) );
path.add(hm);
}
}
}
routes.add(path);
}
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e){
}
答案 4 :(得分:0)
尝试以下代码:
try {
StringRequest sr = new StringRequest(Request.Method.GET, uri, (String) null, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pDialog.dismiss();
;
Log.d("", ".......response====" + response.toString());
////////
try {
JSONObject object = new JSONObject(response);
if (serverCode.equalsIgnoreCase("0")) {
}
if (serverCode.equalsIgnoreCase("1")) {
try {
if ("1".equals(serverCode))
{
JSONArray jsonArray = object.getJSONArray("routes");
if(jsonArray.length()>0)
{
for(int i=0; i<jsonArray.length(); i++)
{
JSONArray legs =jsonArray.getJSONArray("legs");
if(legs.length()>0)
{
for(int i=0; i<legs.length(); i++)
{
JSONObject object1 = legs.getJSONObject("distance");
String distance = object1.getString("text");
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
;
// VolleyLog.d("", "Error: " + error.getMessage());
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Toast.makeText(ForgotPasswordActivity.this, "Timeout Error",
Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
VolleyLog.d("", "" + error.getMessage() + "," + error.toString());
} else if (error instanceof ServerError) {
VolleyLog.d("", "" + error.getMessage() + "," + error.toString());
} else if (error instanceof NetworkError) {
VolleyLog.d("", "" + error.getMessage() + "," + error.toString());
} else if (error instanceof ParseError) {
VolleyLog.d("", "" + error.getMessage() + "," + error.toString());
}
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_email", email);
return params;
}
};
sr.setShouldCache(true);
sr.setRetryPolicy(new DefaultRetryPolicy(50000 * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(sr);
} catch (Exception e) {
e.printStackTrace();
}