我试图从这个json中检索html_instructions,问题是html_instructions在数组内部"步骤"这是在阵列内部"腿"这是在数组内部"路由",我如何得到" html_instructions"
Json的某些部分:
{
geocoded_waypoints: [],
routes: [
{
bounds: {
northeast: {
lat: 39.4798194,
lng: -8.529889899999999
},
southwest: {
lat: 39.2591911,
lng: -8.7332564
}
},
copyrights: "Dados do mapa ©2017 Google, Inst. Geogr. Nacional",
legs: [
{
distance: {
text: "44,6 km",
value: 44551
},
duration: {
text: "30 min.",
value: 1818
},
end_address: "Torres Novas, Portugal",
end_location: {
lat: 39.47908200000001,
lng: -8.5401204
},
start_address: "Santarém, Portugal",
start_location: {
lat: 39.2846595,
lng: -8.7049071
},
steps: [
{
distance: {
text: "0,3 km",
value: 342
},
duration: {
text: "1 min.",
value: 60
},
end_location: {
lat: 39.2822117,
lng: -8.7036403
},
html_instructions: "Siga <b>sul</b> em direção a <b>N3</b>",
polyline: {
points: "cxwnFtdct@JEVGLCR?PDTFNHTNNNPJPDRDN?JCHCv@i@VQTK`CkF"
},
start_location: {
lat: 39.2846595,
lng: -8.7049071
},
travel_mode: "DRIVING"
},
{
distance: {
text: "0,6 km",
value: 605
},
duration: {
text: "1 min.",
value: 66
},
end_location: {
lat: 39.2772762,
lng: -8.7058033
},
html_instructions: "Vire <b>à direita</b> em direção a <b>N3</b>",
maneuver: "turn-right",
polyline: {
points: "yhwnFv|bt@NFx@XZL\RTNt@h@~C`CpAbAh@b@d@\h@^LJLHHBLDb@JP?JALAJAXCNCDANCLCLCPC`@Gz@KV@F@D@JDND"
},
start_location: {
lat: 39.2822117,
lng: -8.7036403
},
travel_mode: "DRIVING"
},
这是我从java中获得的,它不起作用:
if (jsonStr != null) {
try {
//JSONArray array = new JSONArray(jsonStr);
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray routes = jsonObject.optJSONArray("routes");
if (routes != null) {
JSONArray legs = jsonObject.optJSONArray("legs");
if (legs != null){
JSONArray direcoes = jsonObject.optJSONArray("steps");
if (direcoes != null) {
for (int j = 0; j < direcoes.length(); j++) {
JSONObject jsonObject1 = direcoes.getJSONObject(j);
String Distancia = jsonObject1.getString("distance");
String Duracao = jsonObject1.getString("duration");
String Instrucoes = jsonObject1.getString("html_instructions");
HashMap<String, String> direcao = new HashMap<>();
direcao.put("distance", Distancia);
direcao.put("duration", Duracao);
direcao.put("html_instructions", Instrucoes);
listaDirecoes.add(direcao);
}
}
}
}
} catch (final JSONException e) {
Log.e(TAG, "ERROR: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
首先,你JSON在语法上是不正确的,但我认为这是因为你从整个数据中获取了片段,但是下次请正确地执行此操作或将数据剥离到最低限度的需要重现案件(即&#34;方向&#34;或&#34;持续时间&#34;无关紧要。您只需删除它以便下次有更清晰的画面。
这是我从java中获得的,它不起作用
它无效,因为您在大多数情况下尝试从jsonObject
读取,这是不正确的,因为它引用了层次结构的根。另外,名为jsonObject
,jsonObject1
的变量正在寻找麻烦。你迟早会打错字。因此,要阅读html_instructions
字段中的内容,您需要执行以下步骤(伪代码):
// get the root object
JSONObject root = new JSONObject(jsonStr);
// get the routes (which is array)
JSONArray routes = jsonObject.optJSONArray("routes");
// get the 1st element of that array (which is object)
JSONObject singleRoute = routes.get(0);
// get route's legs array
JSONArray legs = singleRoute.get("legs")
// get its first element
JSONObject singleLeg = legs.get(0);
// get html_instructions
String html = singleLeg.get("html_instructions");
答案 1 :(得分:0)
这将返回路线
的arraylistArrayList<LatLng> route
public abstract class DirectionsAsync extends AsyncTask<Void, Void, List<List<HashMap<String, String>>>> {
private JSONObject jsonObject;
public DirectionsAsync(JSONObject jResponse) {
this.jsonObject = jResponse;
}
@Override
protected List<List<HashMap<String, String>>> doInBackground(Void... voids) {
List<List<HashMap<String, String>>> routes = new ArrayList<>();
try {
JSONArray jRoutes = jsonObject.getJSONArray("routes");
/** Traversing all routes */
for (int i = 0; i < jRoutes.length(); i++) {
JSONArray jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
List path = new ArrayList<>();
/** Traversing all legs */
for (int j = 0; j < jLegs.length(); j++) {
JSONArray 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<>();
hm.put("lat", Double.toString(list.get(l).latitude));
hm.put("lng", Double.toString(list.get(l).longitude));
path.add(hm);
}
}
routes.add(path);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
List<HashMap<String, String>> path = result.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
}
onGetRoute(points);
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
public abstract void onGetRoute(ArrayList<LatLng> route);
}
我认为你想要卓尔路线所以使用下面的代码执行上面的类
new DirectionsAsync(jResponse) {//pass here json response got by google
@Override
public void onGetRoute(ArrayList<LatLng> route) {
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(10).color(Color.BLUE).addAll(route);
if (polyLine != null) polyLine.remove();
polyLine = mMap.addPolyline(polylineOptions);
polyLine.setPoints(route);
}
}.execute();
答案 2 :(得分:0)
您的JSON不正确,但如果只是打字错误。 用这个。
JSONObject root = new JSONObject(jsonStr);
JSONArray routes = jsonObject.getJSONArray("routes");
if(routes!=bull && routes.lenght()>0);{
JSONObject singleRoute = routes.get(0);
JSONArray legs = singleRoute.get("legs")
if(legs!=bull && legs.lenght()>0);{
JSONObject singleLeg = legs.get(0);
String html = singleLeg.get("html_instructions");
}
}