Я хочу получить строкуInd, destinationName и timeToStation из этого вызова api https://api.tfl.gov.uk/Line/25,86,w19/Arrivals?stopPointId=490009219W&app_id=&app_key= Может ли кто-нибудь помочь с примером, пожалуйста? [ { "$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" } ]
Разбор массива JSON объектов в android
Мои AsyncTask дается belw
@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);`
Может кто-нибудь помочь мне пожалуйста?
Вы уверены, что здесь ваш JSON 'Log.i («JSON», JSON .toString()); 'logged? Вы делаете все неправильно. –
Это просто, чтобы убедиться, что это не нулевой ответ. Я не понимаю, как получить доступ к объектам внутри массива, поскольку мой doInBackground возвращает объект, и я не уверен, как получить доступ к значениям внутри массива. –
Я добавил его в качестве ответа, я использовал 0 для простого извлечения первого объекта, но вы можете его зацикливать и для извлечения других. И добавил некоторые комментарии, чтобы прояснить их. Надеюсь, что это поможет тебе. Не стесняйтесь спрашивать, есть ли у вас некоторые недоумения. –