简体   繁体   中英

Confused to parsing json c# using json.net

I want to parse this string into ac# class object. I actually have another problem why it has \\r\\n, I save those string onto isolatedstoragefile first, then that \\r\\n is there. Thanks for your help, I have run out of ideas and I barely can remember what I have tried so far.

"{\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}"

Assuming that you already have JSON.NET in your project, you can create a class to represent your data like this:

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; }
}

For any other changes, you can easily modify or add JsonProperty attributes to modify the property binding to a specific part of the JSON document. Also make sure that the types ( int , string , bool ) match up with what is being returned.

Then, all you have to do is invoke:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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