我是編程新手,我想構建一個應用程序,它從Google地圖方向獲取數據,並且第一個用戶需要在列表視圖中獲取可用路線。如何獲取多個對象和數組的JSON數據
我的JSON數據是這樣的
{
geocoded_waypoints: [],
routes: [
{
bounds: {
},
copyrights: "Map data ©2017 Google",
legs: [
{
arrival_time: {},
departure_time: {},
distance: {},
duration: {
text: "25 mins",
value: 1514
},
end_address: "destination address, Croatia",
end_location: {},
start_address: "start address",
start_location: {},
steps: [
{
distance: {},
duration: {},
end_location: {},
html_instructions: "Walk to Vjesnik",
polyline: {},
start_location: {},
steps: [],
travel_mode: "WALKING"
},
{
distance: {},
duration: {},
end_location: {},
html_instructions: "Tram towards Dubec",
polyline: {},
start_location: {},
transit_details: {
arrival_stop: {},
arrival_time: {},
departure_stop: {},
departure_time: {},
headsign: "Dubec",
line: {
agencies: [
{
name: "Zagrebački Električni Tramvaj",
phone: "011 385 60 100 001",
url: "http://www.zet.hr/"
}
],
color: "#ffffff",
name: "Savski most - Dubec",
short_name: "4",
text_color: "#400000",
vehicle: {}
},
num_stops: 9
},
travel_mode: "TRANSIT"
},
這是在數組中的第一條路線,
我也找到了一些代碼,但我不剪這是正確的,我需要去
duration: {
text: "25 mins",
value: 1514
},
- 這是腿陣列
然後在步驟數組中,我需要對象transit_details女巫有2個對象行和num_stops,並在對象行中,我有對象:名稱,短名稱和陣列機構。
我不確定這是否是正確的代碼形式我的情況,我意識到我需要得到兩個數組,父母和孩子。 我很抱歉,如果這個問題已經被問到,但你能指導我如何獲得這些數據?我發現
Java代碼是這樣的,但我並修改它,已經把一些意見:
public class DirectionsJSONParser {
/** Receives a JSONObject and returns a list of lists containing latitude and longitude */
public List<List<HashMap<String,String>>> parse(JSONObject jObject){
List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
JSONArray jRoutes = null;
JSONArray jLegs = null;
JSONObject jSteps = 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 path = new ArrayList<HashMap<String, String>>();
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++){
jSteps = ((JSONObject)jLegs.get(j)).getJSONArray("steps");
jLegs = ((JSONObject)jRoutes.get(j)).getJSONArray("duration"); // --> this is my
/** 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 list = decodePoly(polyline);
jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("transit_details"); // this is my
jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("line"); // this is my
jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("name"); // this is my
jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("short_name"); // this is my
}
routes.add(path);
}
}
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e){
}
return routes;
}
/**
* Method to decode polyline points
* Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
* */
private List decodePoly(String encoded) {
List 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;
}
}
我沒有得到我所需要的數據。
感謝您的幫助。
看看這個博客http://wptrafficanalyzer.in/blog/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/你會得到距離和持續時間兩個 – gaurang
發送完成響應 –