c#将json反序列化为对象列表

时间:2016-02-11 18:53:44

标签: c# json

目前我正在尝试将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; }
    }

4 个答案:

答案 0 :(得分:1)

http://jsonutils.com/ - 问题出在你的班级结构中。此工具根据您提供给它的json数据定义类及其结构。非常便利。为您提供基线,您需要做的就是根据您的特定需求编辑输出。

在您的结构支持对整个字符串进行反序列化后,只需按照之前的顺序进行反序列化,然后您就会发现问题本身已经解决。

然而 - 为清楚起见,您需要一个根源&#39; class包含List<>的定义 - 在此示例中为List<Sorty> - 这将允许您选择的反序列化程序填充Sorty class objects,然后将这些对象添加为新List }&#39; s元素。

以下仅为示例,请使用工具(如上面链接的工具)作为有效的类结构。

//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; }
}

希望有意义,看看那个网站。真的很有帮助。

答案 1 :(得分:1)

您可以使用以下命令对其进行反序列化:

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,并且由于应用为我插入JsonProperty注释而不是将所有属性设置为小写,因此优先于json2csharp。

这是反序列化JSON的结构。

enter image description here

答案 2 :(得分:0)

将字符串解析为Json对象 -

<th>

这是您的主类 -

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();

等等...

答案 3 :(得分:0)

  

使用newtonsoft

public class Flight
{
        public string begin_date {get;set;}
        public string end_date {get;set;}
        public string query_type {get;set;}
        public List<Sorties> sorties{get;set;}
        public string sum_dt {get;set;}
        public string first_tkof {get;set;}
        public string last_ldg {get;set;}
        public string max_dalt {get;set;}
}

public class Sorties
{
    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;}


}
public class Tkof
{
    public string time {get;set;}
    public string loc {get;set;}
    public string rwy {get;set;}
}
public class Ldg
{
    public string time {get;set;}
    public string loc {get;set;}
    public string rwy {get;set;}
}

Usage
var json = "....."
var flightsList = JsonConvert.DeserializeObject<Flight>(json);
var sorties = flightsList.sorties;

foreach(var sorty in sorties)
{
 //use object
}