我從transportapi.com返回數據&將它加載到對象中,但未包含json數組內的數據。C#中的對象(帶數組)的對象#
傳回的JSON:
{
"atcocode": "490012745J",
"smscode": "47889",
"request_time": "2016-11-11T22:10:42+00:00",
"departures": {
"55": [
{
"mode": "bus",
"line": "55",
"line_name": "55",
"direction": "Bakers Arms",
"operator": "TFL",
"date": null,
"expected_departure_date": "2016-11-11",
"aimed_departure_time": null,
"expected_departure_time": "22:19",
"best_departure_estimate": "22:19",
"source": "Countdown instant"
}
]
}
}
控制器代碼:
var source = "http://transportapi.com/v3/uk/bus/stop/490012745J/live.json?api_key=[key]&app_id=[appid]";
Uri sourceUri = new Uri(source);
System.Net.Http.HttpClient sourceClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage sourceResponse = await sourceClient.GetAsync(sourceUri);
var sourceArray = await sourceResponse.Content.ReadAsStringAsync();
var selections = JsonConvert.DeserializeObject<RootObject>(sourceArray);
模型類:
public class BusDepartures
{
public string mode { get; set; }
public string line { get; set; }
public string line_name { get; set; }
public string direction { get; set; }
public string busoperator { get; set; }
public object date { get; set; }
public string expected_departure_date { get; set; }
public object aimed_departure_time { get; set; }
public string expected_departure_time { get; set; }
public string best_departure_estimate { get; set; }
public string source { get; set; }
}
public class Departures
{
// First attempt, use BusDepartures object.
//public List<BusDepartures> BusDepartures { get; set; }
// Second attempt (as "55" is an array), use array, then convert to object later.
public string[] routes { get; set; }
}
public class RootObject
{
public string atcocode { get; set; }
public string smscode { get; set; }
public string request_time { get; set; }
public Departures departures { get; set; }
}
在離境類我曾嘗試和創建BusDepartures對象來存儲離開的細節,但我想知道,因爲它是一個數組,我應該使用r outes數組呢?但是,當逐步執行代碼時,BusDepartures對象(未註釋時)和routes數組都是null。
任何想法?我錯過了什麼?
更新:
感謝botond.botos答案。我修改了我的課程
public class RootObject
{
public string atcocode { get; set; }
public string smscode { get; set; }
public string request_time { get; set; }
public IDictionary<int, IEnumerable<BusDepartures>> departures { get; set; }
}
它工作。
' 「出發」:{ 「55」:[{... }]'無法綁定到模型 - 這意味着'離開'可能需要一個名爲'55'的屬性,該屬性是無效的。如果是「離開」:{「BusDepartures」:[{...}]'那麼它將綁定到'公共列表 BusDepartures {get;組; }' –