我想在C#中获取json解析数据

时间:2015-02-02 08:11:19

标签: c# json

这是我的JObject.Parse字符串。

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

我想在C#中从这个字符串中获取(posts)的值。任何人都可以帮助我如何从这个字符串中获取帖子的值。

2 个答案:

答案 0 :(得分:2)

使用Newtonsoft的Json.net

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

这是一个易于使用的库:

示例:

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

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

string name = m.Name;

答案 1 :(得分:0)

var s=<json_string>;
var firstPost = JObject.Parse(s)["topic"]["posts"][0];
string postBody = (string)firstPost["body"];