【问题标题】:Confused to parsing json c# using json.net对使用 json.net 解析 json c# 感到困惑
【发布时间】:2014-03-21 17:03:30
【问题描述】:

我想将此字符串解析为 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}"

【问题讨论】:

  • “另一个问题”是什么意思?您还没有描述第一个问题 - 如果您不记得到目前为止您已经尝试过什么,我们将很难为您提供帮助。请发布您到目前为止拥有的内容,以及出了什么问题。另请说明您如何看待\r\n - 如果只是在调试器中,那么它们只是为您转义的换行符就在调试器中

标签: c# json windows-phone


【解决方案1】:

假设您的项目中已经有 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);

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2013-07-28
    相关资源
    最近更新 更多