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. and for this I have my code:

 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 tried both of the following methods:

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

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

Json string sownloads is completely fine, but the program crashes during deserialization from the json string. I guess I made a very stupid mistake here, and I can’t understand. Can anyone help me in this regard? Thanks in advance.

+4
source share
2 answers

u just exclude Posts when deserializing , if u wants to deserialize internal elements of Posts , than you should mention it in deserialization

try the following:

 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"]); 
+4
source

There are pages to help you create your data model from JSON (although this is not quite as interesting as the F # .. people do). When you paste your JSON into this website and generate a data model, the following classes come 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 think you need to call your parser as follows and extract your attributes from it:

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

Note that you can rename classes as you wish and use these names instead of the generated ones.


+5
source

Source: https://habr.com/ru/post/1492859/


All Articles