目前,我正在尝试将一个json字符串反序列化为一个对象列表。我有这个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; }
}发布于 2016-02-12 03:04:12
http://jsonutils.com/ -问题出在您的类结构中。此工具基于您提供给它的json数据定义类及其结构。非常方便。给你一个基线来处理,然后你所要做的就是根据你的特定需求编辑输出。
在您的结构支持整个字符串的反序列化之后,只需像以前那样反序列化,您就会发现问题已经自行解决了。
然而,为了清楚起见,您需要一个包含List<>定义的“根”class -在本例中为List<Sorty> -这将允许您选择的反序列化器填充Sorty class objects,然后将这些对象添加为新的List元素。
下面仅仅是说明性的,请使用一个有效的类结构的工具(像上面链接的那个)。
//Root Class
public class Flight
{
public string begin_date { get; set; }
public string end_date { get; set; }
public string query_type { get; set; }
public IList<Sorty> sorties { get; set; } //Bam.. how it works
public string sum_dt { get; set; }
public string first_tkof { get; set; }
public string last_ldg { get; set; }
public int max_dalt { get; set; }
}
//That list contains populated objects of this Sorty class
public class Sorty
{
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 Tkof tkof { get; set; }
public Ldg ldg { get; set; }
public string dalt { get; set; }
public string dt { get; set; }
}希望这是有意义的,请查看该网站。真的很有用。
发布于 2016-02-12 03:24:25
您可以使用以下代码对其进行反序列化:
void Main()
{
const string testJson = @"{""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}";
Root root = JsonConvert.DeserializeObject<Root>(testJson);
}
public class Tkof
{
[JsonProperty("time")]
public string Time { get; set; }
[JsonProperty("loc")]
public string Loc { get; set; }
[JsonProperty("rwy")]
public string Rwy { get; set; }
}
public class Ldg
{
[JsonProperty("time")]
public string Time { get; set; }
[JsonProperty("loc")]
public string Loc { get; set; }
[JsonProperty("rwy")]
public string Rwy { get; set; }
}
public class Sorty
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("cs")]
public string Cs { get; set; }
[JsonProperty("launch")]
public string Launch { get; set; }
[JsonProperty("tow_id")]
public string TowId { get; set; }
[JsonProperty("tow_name")]
public string TowName { get; set; }
[JsonProperty("type")]
public int Type { get; set; }
[JsonProperty("date")]
public string Date { get; set; }
[JsonProperty("tkof")]
public Tkof Tkof { get; set; }
[JsonProperty("ldg")]
public Ldg Ldg { get; set; }
[JsonProperty("dalt")]
public string Dalt { get; set; }
[JsonProperty("dt")]
public string Dt { get; set; }
}
public class Root
{
[JsonProperty("begin_date")]
public string BeginDate { get; set; }
[JsonProperty("end_date")]
public string EndDate { get; set; }
[JsonProperty("query_type")]
public string QueryType { get; set; }
[JsonProperty("sorties")]
public Sorty[] Sorties { get; set; }
[JsonProperty("sum_dt")]
public string SumDt { get; set; }
[JsonProperty("first_tkof")]
public string FirstTkof { get; set; }
[JsonProperty("last_ldg")]
public string LastLdg { get; set; }
[JsonProperty("max_dalt")]
public int MaxDalt { get; set; }
}我通常使用Xamasoft Json Class Generator而不是json2csharp,因为应用程序会为我插入JsonProperty注释,而不是将所有属性都设置为小写。
以下是反序列化的JSON的结构。

发布于 2016-02-12 03:17:11
将字符串解析为Json对象-
JObject jsonObj = JObject.Parse(string);这是你的主类-
public class RootObject
{
public string begin_date { get; set; }
public string end_date { get; set; }
public string query_type { get; set; }
public List<Sorty> sorties { get; set; }
public string sum_dt { get; set; }
public string first_tkof { get; set; }
public string last_ldg { get; set; }
public int max_dalt { get; set; }
}创建类的对象-
RootObject obj = new RootObject();然后您可以按如下方式设置这些值:
obj.begin_date = jsonObj.Value<string>("begin_date");
obj.end_date = jsonObj.Value<string>("end_date");
obj.query_type = jsonObj.Value<string>("query_type");以此类推。
https://stackoverflow.com/questions/35347665
复制相似问题