我对Android开发很陌生。我正在尝试从以下 JSON 代码段中解析"坐标"作为标记,将"名称线"解析为相应坐标的标题:
[{
"osm_id": "45737878",
"jsongeoms": [{
"longitude": 4.379361,
"latitude": 45.4325822004212
}, {
"longitude": 4.3798922,
"latitude": 45.43297760042115
}, {
"longitude": 4.3799371,
"latitude": 45.43302460042115
}, {
"longitude": 4.3800508,
"latitude": 45.433070900421136
}],
"way": "0102000020E610000004000000ED82C1357784114005D281DA5EB746409F05FC7502851140CDDA5CCF6BB74640F9252C3B0E8511400E96A0596DB74640494673092C8511403B1405DE6EB74640",
"highway": "residential",
**"nameline"**: "Rue Vaucanson",
"maxspeed": null
}, {
"osm_id": "272992421",
"jsongeoms": [{
"longitude": 4.3803678,
"latitude": 45.432611700421205
}, {
"longitude": 4.380298399999999,
"latitude": 45.43271530042118
}, {
"longitude": 4.3800861,
"latitude": 45.43302760042115
}, {
"longitude": 4.3800508,
"latitude": 45.433070900421136
}, {
"longitude": 4.3799992,
"latitude": 45.43315010042112
}, {
"longitude": 4.3796751,
"latitude": 45.433588600421075
}, {
"longitude": 4.3796212,
"latitude": 45.43364890042106
}], "way": "0102000020E6100000070000004ECEF5227F8511408896F8D15FB746406EE29AF16C851140D6E4073763B7464020A8644A358511407F09CB726DB74640494673092C8511403B1405DE6EB746405006A2821E851140AFC8657671B74640106CA68CC98411400C5DCDD47FB746401D797B6BBB8411409EA0A2CE81B74640",
"highway": "secondary",
**"nameline"**: "Boulevard Martin Bernard",
"maxspeed": null
},
...
]
但我的输出并不令人满意。
请参阅下面的代码,并告诉我哪里出错了:
JsonArrayRequest request= new JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
JSONArray jsonArray = jsonObject.getJSONArray("jsongeoms");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject jsongeoms = jsonArray.getJSONObject(i);
double longitude = jsongeoms.getDouble("longitude");
double latitude = jsongeoms.getDouble("latitude");
LatLng sainte = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sainte).title("Service Result"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sainte));
mMap.getMaxZoomLevel();
}
/* Enter code here */
}
}
}
}
JsonArrayRequest request= new JsonArrayRequest(Request.Method.GET,url,null,
new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray response) {
for (int i= 0;i< response.length();i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
String naming= jsonObject.getString("nameline");
String highway= jsonObject.getString("highway");
String speed = jsonObject.getString("maxspeed");
String way = jsonObject.getString("way");
JSONArray jsonArray = jsonObject.getJSONArray("jsongeoms");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject jsongeoms = jsonArray.getJSONObject(j);
double longitude = jsongeoms.getDouble("longitude");
double latitude = jsongeoms.getDouble("latitude");
LatLng sainte = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(sainte).title(naming)
.snippet(highway.concat(speed).concat(way))
.icon(bitmapDescriptorFromVector(getApplicationContext(),R.drawable.ic_play_arrow_black_24dp)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sainte));
mMap.getMaxZoomLevel();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},