在android中解析对象的JSON数组 [英] Parsing JSON array of objects in android
问题描述
我想从这个 api 调用中获取 lineId、destinationName 和 timeToStation 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",操作类型":1,"vehicleId": "BJ11DSX","naptanId": "490009219W","stationName": "小伊尔福德巷","lineId": "25","lineName": "25","平台名称": "B","direction": "入站","轴承": "245","destinationNaptanId": "","destinationName": "牛津马戏团","时间戳": "2016-04-17T16:56:56.463Z",时间到站":1534,当前位置": "","towards": "East Ham or Manor Park","expectedArrival": "2016-04-17T17:22:31Z","timeToLive": "2016-04-17T17:23:01Z","modeName": "总线"},{"$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities","id": "1992301652",操作类型":1,"vehicleId": "BJ11DVA","naptanId": "490009219W","stationName": "小伊尔福德巷","lineId": "25","lineName": "25","平台名称": "B","direction": "入站","轴承": "245","destinationNaptanId": "","destinationName": "牛津马戏团","时间戳": "2016-04-17T16:56:56.463Z",时间到站":1159,当前位置": "","towards": "East Ham or Manor Park","expectedArrival": "2016-04-17T17:16:16Z","timeToLive": "2016-04-17T17:16:46Z","modeName": "总线"},{"$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities","id": "733078946",操作类型":1,"vehicleId": "BJ11DVG","naptanId": "490009219W","stationName": "小伊尔福德巷","lineId": "25","lineName": "25",平台名称":B","direction": "入站","轴承": "245","destinationNaptanId": "","destinationName": "牛津马戏团","时间戳": "2016-04-17T16:56:56.463Z",时间到站":790,当前位置": "","towards": "East Ham or Manor Park","expectedArrival": "2016-04-17T17:10:07Z","timeToLive": "2016-04-17T17:10:37Z","modeName": "总线"}]
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?
[
{
"$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 被赋予了 belw
My AsyncTask is given 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 String,
First be sure you have retrieved JSON String,
然后你可以很容易地访问它们里面的属性
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();
}
这篇关于在android中解析对象的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!