gpt4 book ai didi

c# - 获取 Facebook Wall Facebook C# SDK -> 嵌套 JSON

转载 作者:行者123 更新时间:2023-11-30 22:32:05 25 4
gpt4 key购买 nike

我正在尝试获取 facebook 墙,但我遇到了 JSON 嵌套结构的问题。

以下是我的代码

var wall = (JsonObject)fb.Get("me/feed");
List<FBWall> myWall = new List<FBWall>();
foreach (var obj in (JsonArray)wall["data"])
{
FBWall myWall = new Wall();
myWall.Id = (string)(((JsonObject)obj)["id"]);

}

墙上的实际绳子是这样的

{"data":[{"id":"576376893_10150590188751894","from":{"name":"Afnan Bashir","id":"576376893"},"story":"\"how are you man??? \" on Xain Ul-Abiddin's timeline."........ and so on

现在我得到了数据,但是当我得到数据时(它应该是这样的,因为主文件包含其他 json 数组)

(((JsonObject)obj)["from"]) 我得到 {"name":"Afnan Bashir","id":"576376893"}

现在您将如何在 foreach 中编写以获取所有内容而无需转到另一个 foreach。我在想某种方式可能与 Linq

最佳答案

试试这个

public class Post
{
public string ID { get; set; }
public BaseUser From { get; set; }
public string Text { get; set; }
public ItemType Type { get; set; }
public string Picture { set; get; }
public string Link { set; get; }
public string CreatedTime { get; set; }
public string UpdatedTime { get; set; }
}

public class BaseUser
{
public string ID { get; set; }
public string Name { get; set; }
}

public enum ItemType
{
Story,
Message
}
try
{
List<Post> posts = new List<Post>();
dynamic p = (IDictionary<string, object>)fb.Get(userID + "/feed");
foreach (dynamic item in p.data)
{
try
{
Dictionary<string, object>.KeyCollection keys = item.Keys;
Post post = new Post();
if (keys.Contains<string>("story"))
{
post.Type = ItemType.Story;
post.Text = item.story;
post.Link = item.link;
post.Picture = item.picture;
}
else if (keys.Contains<string>("message"))
{
post.Type = ItemType.Message;
post.Text = item.message;
//post.Link = user.Link;
//post.Picture = item.picture;
}
keys = null;
post.ID = item.id;
post.UpdatedTime = item.updated_time;
post.CreatedTime = item.created_time;

BaseUser baseUser = new BaseUser();

dynamic from = item.from;
baseUser.ID = from.id;
baseUser.Name = from.name;
post.From = baseUser;
posts.Add(post);
}
catch (Exception)
{}
}
}
catch (Exception)
{}

关于c# - 获取 Facebook Wall Facebook C# SDK -> 嵌套 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com