2016-02-11 109 views
0

目前我嘗試反序列化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; } 
    } 
+0

你可以試試這個:http://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/ – elevine

+1

順便說一句,你應該真的養成給你的屬性有意義的名字的習慣。代碼通常只寫一次 - 閱讀很多。人們會立即讀出你的代碼是否知道'tkof','ldg'和'dalt'是什麼? –

回答

1

http://jsonutils.com/ - 問題是你的類結構中。這個工具根據你給它的json數據定義類和它們的結構。非常便利。爲您提供一個工作基準,然後您只需編輯輸出以滿足您的特定需求。

您的結構後,支撐整個字符串的反序列化,反序列化只是作爲你之前已經和你會發現,這個問題會得到解決本身。

但是 - 爲清楚起見,你需要一個「根」 class包含List<>的定義 - 在這個例子中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; } 
} 

希望有道理,檢查出該網站。真正有用。

+1

我最喜歡的小json工具之一。 – Jacobr365

0

解析字符串作爲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"); 

等等...

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 
} 
+0

非常感謝! –

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,並且喜歡它了json2csharp由於應用程序爲我插入JsonProperty註釋,inst將所有屬性設置爲小寫字母。

這裏的反序列化JSON的結構。

enter image description here

+0

添加註釋非常方便 - 感謝分享 – Gabe