[英]Parsing JSON array of objects in android
I want to get the lineId, destinationName and timeToStation from this api call https://api.tfl.gov.uk/Line/25,86,w19/Arrivals?stopPointId=490009219W&app_id=&app_key= Can someone help with example please? 我想从此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" } ]
My AsyncTask is given belw 我的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);`
Can someone help me pls? 有人可以帮我吗?
First be sure you have retrieved JSON String, 首先,请确保您已检索JSON字符串,
Then You can easily access the properties inside them like 然后,您可以轻松访问其中的属性,例如
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.