如何将json反序列化为对象

时间:2015-11-03 19:55:36

标签: c# json httpclient

我尝试使用第三个API:

var json = new JsonMediaTypeFormatter();
var apiResponse = response.Content.ReadAsAsync<T>(new MediaTypeFormatter[] { json }).Result;
return response as IList<T>;

类型是夹具:

[Serializable]
[XmlRoot("fixture")]
public class Fixture
{
    public int SportId { get; set; }
    public long Last { get; set; }
    public FixtureLeague[] League { get; set; }
}

FixtureLeague:

[Serializable]
[XmlRoot("league")]
public class FixtureLeague
{
    [XmlAttribute("id")]
    public int Id { get; set; }

    public EventType[] Events { get; set; }
}

的EventType:

[Serializable]
[XmlRoot("events")]
public class EventType
{
    public int Id { get; set; }
    public DateTime Starts { get; set; }
    public string Home { get; set; }
    public string Away { get; set; }
    public string RotNum { get; set; }
    public int LiveStatus { get; set; }
    public string Status { get; set; }
    public int ParlayRestriction { get; set; }
}

JSON:

{"sportId":1,"last":56161443,"league":[{"id":93,"events":[{"id":517144133,"starts":"2015-11-04T02:00:00Z","home":"Jung Soo Byun (KOR)","away":"Eisenhower Lee","rotNum":"5887","liveStatus":0,"status":"H","parlayRestriction":2},{"id":517146919,"starts":"2015-11-04T02:10:00Z","home":"Seong Yeop Han (KOR)","away":"Sony Dwi Kuncoro (INA)","rotNum":"5889","liveStatus":0,"status":"H","parlayRestriction":2},{"id":517069387,"starts":"2015-11-04T00:00:00Z","home":"Ginting Anthony (IND)","away":"Min Hyuk Park (KOR)","rotNum":"5851","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517069530,"starts":"2015-11-04T00:00:00Z","home":"Ji Hoon Hong (KOR)","away":"Na Sung Seung (KOR)","rotNum":"5853","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517069944,"starts":"2015-11-04T00:00:00Z","home":"Ki Hwa Jung (KOR)","away":"Hyeok Jin Jeon (KOR)","rotNum":"5855","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517070911,"starts":"2015-11-04T00:00:00Z","home":"Min Ki Kim (KOR)","away":"Jen Hao Hsu (TPE)","rotNum":"5857","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517071064,"starts":"2015-11-04T00:00:00Z","home":"Wan Ho Son (KOR)","away":"Seung Hoon Woo (KOR)","rotNum":"5859","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517071440,"starts":"2015-11-04T00:00:00Z","home":"Riichi Takeshita (JPN)","away":"Gyun Joo Myung (KOR)","rotNum":"5861","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517071645,"starts":"2015-11-04T00:00:00Z","home":"Kanta Tsuneyama (JPN)","away":"Ji Kwan Um (KOR)","rotNum":"5863","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517072700,"starts":"2015-11-04T00:00:00Z","home":"Yosei Yamaguchi (JPN)","away":"Jung Hoon Park (KOR)","rotNum":"5865","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517072830,"starts":"2015-11-04T00:00:00Z","home":"Jung Kwan Yoon (KOR)","away":"Young Min Kim (KOR)","rotNum":"5867","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517073177,"starts":"2015-11-04T00:00:00Z","home":"Young Woo Choi (KOR)","away":"Bin Qiao (CHN)","rotNum":"5869","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517073609,"starts":"2015-11-04T00:40:00Z","home":"Ha Young Woong (KOR)","away":"Kuo Po Cheng (TPE)","rotNum":"5871","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517074371,"starts":"2015-11-04T00:40:00Z","home":"Brice Leverdez (FRA)","away":"Seung Jae Seo (KOR)","rotNum":"5873","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517075934,"starts":"2015-11-04T00:40:00Z","home":"Teck Zhi Soo (MYS)","away":"Ki Hoon Han (KOR)","rotNum":"5875","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517076255,"starts":"2015-11-04T00:50:00Z","home":"Lee Dong Keun (KOR)","away":"Yu Hsien Lin (TPE)","rotNum":"5877","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517139496,"starts":"2015-11-04T00:50:00Z","home":"Michal Rogalski (POL)","away":"Muhammad Bayu Pangisthu (INA)","rotNum":"5879","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517140996,"starts":"2015-11-04T01:20:00Z","home":"Kai Guo (CHN)","away":"Hoon Kim Dong (KOR)","rotNum":"5881","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517143070,"starts":"2015-11-04T01:20:00Z","home":"Zulfadli Zulkiffli (MAS)","away":"Kyung Deok Shin (KOR)","rotNum":"5883","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517143921,"starts":"2015-11-04T01:30:00Z","home":"Christie Jonatan (SIN)","away":"Dong Ryung Kim (KOR)","rotNum":"5885","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517147568,"starts":"2015-11-04T02:10:00Z","home":"Gyung Bo Ko (KOR)","away":"Kazumasa Sakai (JPN)","rotNum":"5891","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517147780,"starts":"2015-11-04T02:40:00Z","home":"Kenta Nishimoto (JPN)","away":"Geon A. Jo (KOR)","rotNum":"5893","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517148361,"starts":"2015-11-04T02:50:00Z","home":"Ki Kim Min (KOR)","away":"Jen Hao Hsu (CHN)","rotNum":"5895","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517148589,"starts":"2015-11-04T02:50:00Z","home":"Reksy Aureza Megananda (INA)","away":"Seong Hyun Son (KOR)","rotNum":"5897","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517150411,"starts":"2015-11-04T03:20:00Z","home":"Yi Hsiang Yang (CHN)","away":"Hyun Joon Joo (KOR)","rotNum":"5901","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517150769,"starts":"2015-11-04T03:30:00Z","home":"Kwang Hee Heo (KOR)","away":"Ihsan Maulana Mustofa (TPE)","rotNum":"5903","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517150973,"starts":"2015-11-04T03:30:00Z","home":"Yugi Shi (CHN)","away":"Jong Woo Yim (KOR)","rotNum":"5905","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517151304,"starts":"2015-11-04T04:10:00Z","home":"Soon Huat Goh (MYS)","away":"Adrian Dziolko (POL)","rotNum":"5909","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517149604,"starts":"2015-11-04T03:20:00Z","home":"Je Lee Hong (KOR)","away":"Iskandar Zulkarnain Zainuddin (MYS)","rotNum":"5899","liveStatus":0,"status":"O","parlayRestriction":2},{"id":517151124,"starts":"2015-11-04T04:00:00Z","home":"Chi Liu Wei (TPE)","away":"Hyun IL Lee (KOR)","rotNum":"5907","liveStatus":0,"status":"O","parlayRestriction":2}]},{"id":94,"events":[{"id":517168898,"starts":"2015-11-04T05:15:00Z","home":"Kyung Eun Jung/Seung Chan Shin (KOR)","away":"Chan Mi Kim/Bo Ram Lee (KOR)","rotNum":"1351","liveStatus":0,"status":"O","parlayRestriction":2}]},{"id":95,"events":[{"id":517072891,"starts":"2015-11-04T04:40:00Z","home":"Ji Hyun Sung (KOR)","away":"Hsiao Huan Chen (TPE)","rotNum":"5927","liveStatus":0,"status":"H","parlayRestriction":2},{"id":517072754,"starts":"2015-11-04T04:00:00Z","home":"Min Hee Shon (KOR)","away":"Chi Ya Cheng (TPE)","rotNum":"5921","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517072832,"starts":"2015-11-04T04:10:00Z","home":"Thu Huyen Le (VIE)","away":"Hsuan-Yu Wendy Chen (AUS)","rotNum":"5923","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517072857,"starts":"2015-11-04T04:40:00Z","home":"Chiew Sien Lim (MAS)","away":"Hyang Im Kim (TPE)","rotNum":"5925","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517072952,"starts":"2015-11-04T04:50:00Z","home":"Yen Mei Ho (MAS)","away":"Ga Eun Kim (KOR)","rotNum":"5929","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517162431,"starts":"2015-11-04T05:30:00Z","home":"Hyo Min Kim (KOR)","away":"Shih Han Hung (TPE)","rotNum":"5931","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517166520,"starts":"2015-11-04T06:00:00Z","home":"Lin Ying Chun (TPE)","away":"Sun Yu (CHN)","rotNum":"5933","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517166828,"starts":"2015-11-04T06:15:00Z","home":"Sayaka Sato (JPN)","away":"Young Kim Na (KOR)","rotNum":"5935","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517167034,"starts":"2015-11-04T06:45:00Z","home":"Mei Hui Chiang (TPE)","away":"Jana Ciznarova (SVK)","rotNum":"5937","liveStatus":0,"status":"I","parlayRestriction":2},{"id":517167644,"starts":"2015-11-04T07:20:00Z","home":"Kana Ito (JPN)","away":"Dinar Dyah Ayustine (INA)","rotNum":"5939","liveStatus":0,"status":"I","parlayRestriction":2}]},{"id":191545,"events":[{"id":495418854,"starts":"2030-01-01T08:01:00Z","home":"Team1","away":"Team2","rotNum":"1201","liveStatus":0,"status":"I","parlayRestriction":2},{"id":496997901,"starts":"2030-01-01T08:01:00Z","home":"Team3","away":"Team4","rotNum":"1203","liveStatus":0,"status":"I","parlayRestriction":2}]}]}

2 个答案:

答案 0 :(得分:4)

使用Json.NET

Fixture fixture = JsonConvert.DeserializeObject<Fixture>(jsonString);

答案 1 :(得分:0)

使用Newtonsoft.Json.JsonConvert。您可以从NuGet Packages下载Newtonsoft.Json。这是示例代码:

Fixture objFixture = JsonConvert.DeserializeObject<Fixture>(strJson);