我尝试从数据库中添加标记,但是我有一个错误值org.json.jsonobject无法转换为jsonarray
05-12 22:30:18.300 2926-2926/com.example.getgpslocation W/System.err: org.json.JSONException: Value {"markers":[{"lat":"-0.6092131","lng":"35.2236078"},{"lat":"-0.6340827","lng":"35.1907590"},{"lat":"-0.6292730","lng":"35.1914282"},{"lat":"-0.6292730","lng":"35.2102234"},{"lat":"-0.6139107 ","lng":"35.2128532"},{"lat":"-0.6239696","lng":"35.2201147"},{"lat":"-0.6230255","lng":"35.2174852"},{"lat":"-0.6296344","lng":"35.2231999"},{"lat":"-0.6305786","lng":"35.2246723"},{"lat":"-0.6423803","lng":"35.2206756"},{"lat":"-0.6330677","lng":"35.2163282"},{"lat":"-0.6243836","lng":"35.2161752"},{"lat":"-0.6272590","lng":"35.2102847"},{"lat":"-0.6254565","lng":"35.2031314"},{"lat":"-0.6191050","lng":"35.2011677"},{"lat":"-0.6125390","lng":"35.1946448"},{"lat":"-0.6149852","lng":"35.1927510"},{"lat":"-0.6187617","lng":"35.1926107"},{"lat":"-0.6330677","lng":"35.2163282"},{"lat":"-0.6243836","lng":"35.2161752"},{"lat":"-0.6272590","lng":"35.2102847"},{"lat":"-0.6254565","lng":"35.2031314"},{"lat":"-0.6191050","lng":"35.2011677"},{"lat":"-0.6125390","lng":"35.1946448"},{"lat":"-0.6149852","lng":"35.1927510"},{"lat":"-0.6187617","lng":"35.1926107"},{"lat":"-0.6422793","lng":"35.1904012"},{"lat":"-0.6495749","lng":"35.1859469"},{"lat":"-0.6600892","lng":"35.1788616"},{"lat":"-0.6449401","lng":"35.1798438"},{"lat":"-0.6352412","lng":"35.1802647"},{"lat":"-0.6267440","lng":"35.1816327"},{"lat":"-0.6394620","lng":"35.2172748 "}]} of type org.json.JSONObject cannot be converted to JSONArray
这是我的代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_Marker,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
jresponse = jsonArray.getJSONObject(i);
latitude = Double.parseDouble(jresponse.getString("lat"));
longitude = Double.parseDouble(jresponse.getString("lng"));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude,longitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.tram_48))
.draggable(true));
}
} catch (JSONException e) {
e.printStackTrace();
}
//pDialog.dismiss();
}
}
最佳答案
您可能想尝试一下:
try{
JSONObject jsonObj = new JSONObject(response);
JSONArray jsonArray = jsonObj.getJSONArray("markers");
因为据我所知,这应该可行。