我想从我的 JSON 文件中获取“lat”和“lng”并添加到一个保留 GeoPoint 的数组中。我试图这样做,但它对我不起作用:
protected List<GeoPoint> JsonArray(){
List<GeoPoint> endp = new ArrayList<GeoPoint>();
try{
JSONObject obj = new JSONObject(json);
JSONArray steps = obj.getJSONArray("routes");
for(int i=0;i<steps.length();i++){
JSONObject temp = steps.getJSONObject(i);
JSONObject ele = temp.optJSONObject("steps").optJSONObject("end_location");
ele.getJSONObject("lat");
ele.getJSONObject("lng");
double lat = Double.parseDouble(ele.getJSONObject("lat").toString());
double lng = Double.parseDouble(ele.getJSONObject("lng").toString());
endp.add(new GeoPoint((int)(lat *1E6),(int)(lng * 1E6)));
}
}catch (JSONException e) {
// TODO: handle exception
}
return endp;
}
这是我的 JSON 文件外观的一部分:
{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 41.87999000000001,
"lng" : -87.615020
},
"southwest" : {
"lat" : 29.74674000000001,
"lng" : -95.361220
}
},
"copyrights" : "Dane do Mapy ©2013 Google",
"legs" : [
{
"distance" : {
"text" : "1 085 mil",
"value" : 1746457
},
"duration" : {
"text" : "16 godz. 39 min",
"value" : 59955
},
"end_address" : "1362 Chenevert Street, Houston, Teksas 77003, Stany Zjednoczone",
"end_location" : {
"lat" : 29.750110,
"lng" : -95.36016000000001
},
"start_address" : "138-230 South Columbus Drive, Chicago, Illinois 60601, Stany Zjednoczone",
"start_location" : {
"lat" : 41.87999000000001,
"lng" : -87.62075000000002
},
"steps" : [
{
"distance" : {
"text" : "338 stóp",
"value" : 103
},
"duration" : {
"text" : "1 min",
"value" : 9
},
"end_location" : {
"lat" : 41.88090,
"lng" : -87.62069000000001
},
"html_instructions" : "Kieruj się \u003cb\u003eS Columbus Dr\u003c/b\u003e na \u003cb\u003epółnoc\u003c/b\u003e w stronę \u003cb\u003eE Monroe St\u003c/b\u003e",
"polyline" : {
"points" : "}tr~FtlxuOuA@w@@QMUA"
},
"start_location" : {
"lat" : 41.87999000000001,
"lng" : -87.62075000000002
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,2 mil",
"value" : 266
},
"duration" : {
"text" : "1 min",
"value" : 33
},
"end_location" : {
"lat" : 41.88086000000001,
"lng" : -87.61750000000001
},
"html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eE Monroe St\u003c/b\u003e",
"polyline" : {
"points" : "szr~FhlxuO?SAyA?_B@yA?iBAaB?WBE@C@A?C?A@A?C?y@?_@"
},
"start_location" : {
"lat" : 41.88090,
"lng" : -87.62069000000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2,0 mil",
"value" : 3186
},
"duration" : {
"text" : "3 min",
"value" : 180
},
"end_location" : {
"lat" : 41.85320,
"lng" : -87.61470000000001
},
"html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eU.S. 41 S\u003c/b\u003e",
"polyline" : {
"points" : "kzr~FjxwuOpMOZ@`PO|ACvDC@?lDCZAfMGjAA`@Av@?^?P?N@P@j@LRFNDRJRHZRb@\\b@^nAjA@@JJXXb@b@z@v@RRp@`@RLr@Xn@Np@J`A@bBOp@Kv@K@?@?@A@?@?@?@?PEvBWxB]fDq@xA]jF_BTIj@QpGuBd@OBAf@SrDyAPGNG@AVKbA_@~@]|@]z@Y`F{AtCy@fHyB"
},
"start_location" : {
"lat" : 41.88086000000001,
"lng" : -87.61750000000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,4 mil",
"value" : 581
},
"duration" : {
"text" : "1 min",
"value" : 30
},
"end_location" : {
"lat" : 41.848390,
"lng" : -87.614670
},
"html_instructions" : "Zjedź \u003cb\u003eInterstate 55 S\u003c/b\u003e w kierunku \u003cb\u003eSaint Louis\u003c/b\u003e",
"polyline" : {
"points" : "omm~FzfwuOVDPAN?VGlBk@b@M@?JEDAVI`A[dBi@`A[z@UPGZGf@E\\Ab@@VDJBHBTFTHRJPNRNLL\\f@NXLTP\\BF"
},
"start_location" : {
"lat" : 41.85320,
"lng" : -87.61470000000001
},
"travel_mode" : "DRIVING"
},
有谁知道我该怎么做?