简体   繁体   中英

Json Parsing in c# using Json.Net

{"Posts":
          [{"id":"1",
            "title":"Bibidh prothom khondo",
            "content":"sjih sdkljjdsf kdjsfjks",
            "author":"","last_update":"23 june 2013",
            "Comments":
                [{"id":"1",
                  "content":"sjih sdkljjdsf kdjsfjks",
                  "author":"","last_update":"23 june 2013"}]},
            {"id":"2",
             "title":"Bibidh prothom khondo",
             "content":"sjih sdkljjdsf kdjsfjks",
             "author":"",
             "last_update":"24 june 2013",
             "Comments":[{"id":"1","content":"sjih sdkljjdsf kdjsfjks","author":"","last_update":"23 june 2013"}]},{"id":"3","title":"Bibidh prothom khondo","content":"sjih sdkljjdsf kdjsfjks","author":"","last_update":"25 june 2013"}]}

I am trying to parse this json. & For this I my code is:

public class Attributes
    {
        [JsonProperty("id")]
        public string ID { get; set; }
        [JsonProperty("title")]
        public string TITLE { get; set; }
        [JsonProperty("content")]
        public string CONTENT { get; set; }
        [JsonProperty("author")]
        public string AUTHOR { get; set; }
        [JsonProperty("last_update")]
        public string LAST_UPDATE { get; set; }
        [JsonProperty("Comments")]
        public string[] COMMENTS { get; set; }
    }

    public class DataJsonAttributeContainer
    {
        public List<Attributes> attributes { get; set; }
    }

    public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
        return deserializedProduct;
    }

I have tried both of this following way:

var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);

& var container = DeserializeFromJson<List<Attributes>>(e.Result);

Json string sownloads completely fine but program crashes in while deserializing from json string. I guess, I have made a very silly mistake here & I can not figure it out. Can anyone please help me in this regard? Thanks in advance.

There are pages that help you with generating your data model from JSON (though it's not quite as fancy as the way F# folks can do it..). When pasting your JSON on this website and generating the data model the following classes pop out.

public class Comment
{
    public string id { get; set; }
    public string content { get; set; }
    public string author { get; set; }
    public string last_update { get; set; }
}

public class Post
{
    public string id { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public string author { get; set; }
    public string last_update { get; set; }
    public List<Comment> Comments { get; set; }
}

public class RootObject
{
    public List<Post> Posts { get; set; }
}

I guess you have to call your parser then as follows and grab your attributes out of it:

var container = DeserializeFromJson<RootObject>(e.Result);

Please note that you can rename the classes as you wish and use those names instead of the generated ones.


u are simply excluding Posts when deserializing , if u want to deserialize inner items of Posts , than u have to mention it in deserialization

try this :

var parsed = JObject.Parse(e.Result);
var container = DeserializeFromJson<List<Attributes>>(parsed["Posts"]);

or

var parsed = JsonSerializer.DeserializeFromString<Dictionary<string,string>>(e.Result);
var container = DeserializeFromJson<List<Attributes>>(parsed["Posts"]);

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