假设这是Facebook的API JSONRESPONSE(最后)。
到目前为止,我都在对每个牛顿的ID和 created_time
进行宣传,但第二个包括反应,这是列表和注释元素,是另一个列表。
我正在使用以下来循环通过帖子:
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);
和
for each post in posts....
id = post.id
我在循环中如何循环到帖子Reactions
并发布Comments
。
我到目前为止的课程是:
public class FacebookPostData
{
public List<FacebookPost> Data { get; set; }
}
public class FacebookPost
{
public string id { get; set; }
public string created_time { get; set; }
}
API响应是:
{
"data": [{
"id": "",
"created_time": ""
},
{
"id": "",
"created_time": "",
"reactions": {
"data": [{
"id": "",
"name": "",
"type": ""
},
{
"id": "",
"name": "",
"type": ""
}
],
"paging": {
"cursors": {
"before": "",
"after": ""
}
}
},
"comments": {
"data": [{
"created_time": "",
"from": {
"name": "",
"id": ""
},
"message": "",
"id": ""
}],
"paging": {
"cursors": {
"before": "",
"after": ""
}
}
}
}
],
"paging": {
"previous": "",
"next": ""
}
}
谢谢!
您的类可以以这种方式结构:
public class FacebookPostData
{
public List<FacebookPost> data { get; set; }
public Paging3 paging { get; set; }
public FacebookPostData()
{
this.data = new List<FacebookPost>();
this.paging = new Paging3();
}
}
public class FacebookPost
{
public string id { get; set; }
public string created_time { get; set; }
public Reactions reactions { get; set; }
public Comments comments { get; set; }
public FacebookPost()
{
this.reactions = new Reactions();
this.comments = new Comments();
}
}
public class Paging3
{
public string previous { get; set; }
public string next { get; set; }
}
public class Reactions
{
public List<Data2> data { get; set; }
public Paging paging { get; set; }
public Reactions()
{
this.data = new List<Data2>();
this.paging = new Paging();
}
}
public class Data2
{
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public Paging()
{
this.cursors = new Cursors();
}
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class Comments
{
public List<Data3> data { get; set; }
public Paging2 paging { get; set; }
public Comments()
{
this.data = new List<Data3>();
this.paging = new Paging2();
}
}
public class Data3
{
public string created_time { get; set; }
public From from { get; set; }
public string message { get; set; }
public string id { get; set; }
public Data3()
{
this.from = new From();
}
}
public class Paging2
{
public Cursors2 cursors { get; set; }
public Paging2()
{
this.cursors = new Cursors2();
}
}
public class From
{
public string name { get; set; }
public string id { get; set; }
}
public class Cursors2
{
public string before { get; set; }
public string after { get; set; }
}
所以,您可以做这样的事情:
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);
foreach(var post in posts.data)
{
Console.WriteLine(post.id);
// Reactions...
foreach(var reaction in post.reactions.data)
{
Console.WriteLine(reaction.id);
}
// Comments...
foreach(var comment in post.comments.data)
{
Console.WriteLine(comment.id);
Console.WriteLine(comment.from.id);
Console.WriteLine(comment.from.name);
}
}
请参阅此演示。
希望这会有所帮助。