繁体   English   中英

混淆使用json.net解析json c#

[英]Confused to parsing json c# using json.net

我想将此字符串解析为ac#类对象。 我实际上还有一个问题,为什么它有\\ 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,则可以创建一个类来表示数据,如下所示:

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文档特定部分的属性。 还要确保类型( intstringbool )与返回的内容匹配。

然后,您要做的就是调用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM