目前我嘗試反序列化JSON字符串對象的列表。 我有這樣的JSON字符串:C#反序列化JSON到對象列表
{
"begin_date": "2016-02-01",
"end_date": "2016-02-11",
"query_type": "ap",
"sorties": [
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-03",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "23:16",
"loc": "EHTL",
"rwy": "2"
},
"dalt": "20",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-03",
"tkof": {
"time": "23:17",
"loc": "EHTL",
"rwy": "0"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "10",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-04",
"tkof": {
"time": "14:54",
"loc": "EHTL",
"rwy": "32"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "250",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-04",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "23:12",
"loc": "EHTL",
"rwy": "19"
},
"dalt": "10",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "13:05",
"loc": "EHTL",
"rwy": "32"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "0",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "13:19",
"loc": "EHTL",
"rwy": "14"
},
"dalt": "0",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "19:59",
"loc": "EHTL",
"rwy": "23"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "0",
"dt": ""
}
],
"sum_dt": "0",
"first_tkof": "23:17",
"last_ldg": "13:19",
"max_dalt": 250
}
我想所有的架次存儲到我已經創建的對象的列表。 所以我可以輕鬆地使用它。 我知道我可以使用Newtonsoft.Json.JsonConvert.DeserializeObject,但我只知道如何在非嵌套的json字符串上做到這一點。
public class Flight
{
public string id { get; set; }
public string cs { get; set; }
public string launch { get; set; }
public string tow_id { get; set; }
public string tow_name { get; set; }
public int type { get; set; }
public string date { get; set; }
public string tkof { get; set; }
public string ldg { get; set; }
public string dalt { get; set; }
public string dt { get; set; }
}
你可以試試這個:http://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/ – elevine
順便說一句,你應該真的養成給你的屬性有意義的名字的習慣。代碼通常只寫一次 - 閱讀很多。人們會立即讀出你的代碼是否知道'tkof','ldg'和'dalt'是什麼? –