我想从此api调用https://api.tfl.gov.uk/Line/25,86,w19/Arrivals?stopPointId=490009219W&app_id=&app_key=获取lineId,destinationName和timeToStation
有人可以帮忙举例吗?
[
{
"$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities",
"id": "-480785385",
"operationType": 1,
"vehicleId": "BJ11DSX",
"naptanId": "490009219W",
"stationName": "Little Ilford Lane",
"lineId": "25",
"lineName": "25",
"platformName": "B",
"direction": "inbound",
"bearing": "245",
"destinationNaptanId": "",
"destinationName": "Oxford Circus",
"timestamp": "2016-04-17T16:56:56.463Z",
"timeToStation": 1534,
"currentLocation": "",
"towards": "East Ham or Manor Park",
"expectedArrival": "2016-04-17T17:22:31Z",
"timeToLive": "2016-04-17T17:23:01Z",
"modeName": "bus"
},
{
"$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities",
"id": "1992301652",
"operationType": 1,
"vehicleId": "BJ11DVA",
"naptanId": "490009219W",
"stationName": "Little Ilford Lane",
"lineId": "25",
"lineName": "25",
"platformName": "B",
"direction": "inbound",
"bearing": "245",
"destinationNaptanId": "",
"destinationName": "Oxford Circus",
"timestamp": "2016-04-17T16:56:56.463Z",
"timeToStation": 1159,
"currentLocation": "",
"towards": "East Ham or Manor Park",
"expectedArrival": "2016-04-17T17:16:16Z",
"timeToLive": "2016-04-17T17:16:46Z",
"modeName": "bus"
},
{
"$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities",
"id": "733078946",
"operationType": 1,
"vehicleId": "BJ11DVG",
"naptanId": "490009219W",
"stationName": "Little Ilford Lane",
"lineId": "25",
"lineName": "25",
"platformName": "B",
"direction": "inbound",
"bearing": "245",
"destinationNaptanId": "",
"destinationName": "Oxford Circus",
"timestamp": "2016-04-17T16:56:56.463Z",
"timeToStation": 790,
"currentLocation": "",
"towards": "East Ham or Manor Park",
"expectedArrival": "2016-04-17T17:10:07Z",
"timeToLive": "2016-04-17T17:10:37Z",
"modeName": "bus"
}
]
我的AsyncTask给出了
@Override
protected JSONObject doInBackground(String... args){
JSONParser jsonParser = new JSONParser();
Log.i("URL", url);
JSONObject json = jsonParser.getJSONFromUrl(url);
if(json == null) {
Log.i("Json obj =" , "NULL");
}
else{
return json;
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject json){
progressDialog.dismiss();
//String shopName ="";
// String distance="";
try{
//Fetching JSON Array
Log.i("JSON", json.toString());
jsonData = new JSONArray(json);
Double arrivalTime= 0.0;
for(int i=0;i<json.length();i++) {
try {
JSONObject c = json.getJSONObject(i);
busNoArray.add(json.getString(TAG_LINEID));
destinationArray.add(json.getString(TAG_DESTINATION));
arrivalTime = json.getDouble(TAG_TIME) / 60;
arrivalTimeArray.add(arrivalTime);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter = new BusTimeAdapter(BusTimeActivity.this,busNoArray, destinationArray, arrivalTimeArray);
mListView.setAdapter(adapter);`
有人可以帮我吗?
答案 0 :(得分:0)
首先确保您已检索到JSON字符串
然后您可以轻松访问其中的属性,如
try {
JSONArray jsonArray = new JSONArray(json);
JSONObject obj = jsonArray.getJSONObject(0); //0 for just retrieving first object you can loop it
String myVehicleID = obj.getString("vehicleId"); //To retrieve vehicleId
//Similarly do it for others as well
} catch (JSONException e) {
e.printStackTrace();
}