-6

This is my JObject.Parse string.

{"topic":{"account_id":190884,"created_at":"2015-01-31T16:35:59+05:00","delta":true,"forum_id":5000225377,"hits":0,"id":5000025527,"import_id":null,"last_post_id":5000040596,"locked":false,"merged_topic_id":null,"posts_count":0,"published":true,"replied_at":"2015-01-31T16:35:59+05:00","replied_by":5005399997,"stamp_type":null,"sticky":0,"title":"I want use API of Freshdesk.","updated_at":"2015-01-31T16:35:59+05:00","user_id":5005399997,"user_votes":0,"posts":[{"account_id":190884,"answer":false,"body":" I want to use API of Freshdesk to get forums of Freshdesk. I want to access users and posts from different Forums and show that data in my Application. I want to develop C# console Application.\u00a0  Anyone please help me. \r\n","body_html":"\u003Cp\u003EI want to use API of Freshdesk to get forums of Freshdesk. I want to access users and posts from different Forums and show that data in my Application. I want to develop C# console Application.\u00a0\u003C/p\u003E\u003Cp\u003EAnyone please help me.\u003C/p\u003E\r\n","created_at":"2015-01-31T16:35:59+05:00","forum_id":5000225377,"id":5000040596,"import_id":null,"published":true,"spam":null,"topic_id":5000025527,"trash":false,"updated_at":"2015-01-31T16:35:59+05:00","user_id":5005399997}]}}

I want to get values of (posts) from this string in C#. Anyone can help me how i can get values of posts from this string.

AHMAD SUMRAIZ
  • 535
  • 8
  • 21
awais
  • 1
  • 1

2 Answers2

2

Use Newtonsoft's Json.net

https://www.nuget.org/packages/Newtonsoft.Json/

It is an easy to use library:

Example:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
  'Action',
  'Comedy'
]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
Herr Kater
  • 3,242
  • 2
  • 22
  • 33
  • I include newtonsoft in my application. but it's not helpful for me. I also try this. In test there is my string. here is my other classes. public class Topic { public int account_id { get; set; } public ICollection posts { get; set; } } public class Post { public int account_id { get; set; } public bool answer { get; set; } public string body { get; set; } public string body_html { get; set; } } Topic ABC = JsonConvert.DeserializeObject(test); when i debug it and check the value of ABC. there is null value shows. – awais Feb 02 '15 at 09:35
  • In the given string there is an extra level which contains the "topic" object. Add this class: public class Root{public Topic topic { get; set; }} and then just call Root ABC = JsonConvert.DeserializeObject(test); – Herr Kater Feb 03 '15 at 14:34
  • This will not work in .netcore – vijay Nov 15 '19 at 12:00
1
var s=<json_string>;
var firstPost = JObject.Parse(s)["topic"]["posts"][0];
string postBody = (string)firstPost["body"];
wezten
  • 2,126
  • 3
  • 25
  • 48