对使用json.net解析json c#感到困惑

本文关键字:json 解析 net | 更新日期: 2023-09-27 17:51:12

我想把这个字符串解析成一个c#类对象。实际上我还有另一个问题,为什么它有'r'n,我先把这些字符串保存到isolatedstoragefile中,然后'r'n就在那里了。谢谢你的帮助,我已经想不起来了,到目前为止我都试过什么了。

"{'r'n  '"chunks'": ['r'n    '"Moyes insists he has felt no pressure from above at Old Trafford while the hierarchy insisted their position had not changed after Sunday.'",'r'n    '"Moyes has had plenty of criticism this season so it would be unfair not to give him credit where it is due.'",'r'n    '"Should Manchester City pitch up at Old Trafford next Tuesday and treat them with the same contempt as Liverpool did in the 3-0 loss on Sunday the questions surrounding Moyes will return.'",'r'n    '"''n''nMoyes will exercise caution - but after so many miserable moments this season he fully deserved his finest night since taking over from Ferguson.'",'r'n    '"It had to be because anything other than a passage into the Champions League quarter-finals by beating a mediocre Olympiakos would have increased the pressure on his position at Old Trafford.'"'r'n  ],'r'n  '"id'": 87,'r'n  '"interest'": '"Football'",'r'n  '"interest_id'": 2,'r'n  '"main_image'": '"http://news.bbcimg.co.uk/media/images/73692000/jpg/_73692749_a3f7341f-30c2-404e-a384-0478c4e6f9a0.jpg'",'r'n  '"published_at'": 1395299199,'r'n  '"publisher_id'": 5,'r'n  '"publisher_name'": '"BBC - Football'",'r'n  '"source_url'": '"http://www.bbc.co.uk/sport/0/football/26658237'",'r'n  '"title'": '"Man Utd rally gives respite to Moyes'"'r'n}"

对使用json.net解析json c#感到困惑

假设您已经有JSON。. NET中,您可以创建一个类来表示您的数据,如下所示:

public class GiveItAName
{
    [JsonProperty("chunks")]
    public List<string> Chunks { get; set; }
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("interest")]
    public string Interest { get; set; }
    [JsonProperty("interest_id")]
    public int InterestId { get; set; }
    [JsonProperty("main_image")]
    public string MainImage { get; set; }
    [JsonProperty("published_at")]
    public long PublishedAt { get; set; }
    [JsonProperty("publisher_id")]
    public int PublisherId { get; set; }
    [JsonProperty("publisher_name")]
    public string PublisherName { get; set; }
    [JsonProperty("source_url")]
    public string SourceUrl { get; set; }
    [JsonProperty("title")]
    public string Title { get; set; }
}

对于任何其他更改,您可以轻松地修改或添加JsonProperty属性,以修改属性绑定到JSON文档的特定部分。还要确保类型(int, string, bool)与返回的内容匹配。

然后,你所要做的就是调用:

GiveItAName deserializedJson = JsonConvert.DeserializeObject<GiveItAName>(responseContent);