JsonObject模型Facebook SDK

Rof*_*ers 8 c# json json.net .net-3.5 facebook-c#-sdk

我必须使用facebook c#sdk作为.net 3.5中的新项目,我知道最新版本有4个示例 - 但它也是针对3.5编译的,因此完全可以正常工作.

无论如何,如果我非常愚蠢,请原谅我.但我正在寻找将json对象转换为我的模型,我可以这样做吗?

public ActionResult About()
{
    var app = new FacebookApp();
    JsonObject friends = (JsonObject)app.Get("me/friends");
    ViewData["Albums"] = new Friends((string)friends.ToString());
    return View();
}

public class Friends
{
    public string name { get; set; }
    public string id { get; set; }

    public Friends(string json)
    {
        JArray jObject = JArray.Parse(json);
        JToken jData = jObject["data"];

        name = (string)jData["name"];
        id = (string)jData["id"];
    }
}
Run Code Online (Sandbox Code Playgroud)

这是使用Json.Net.显然这不起作用,我得到的错误是

从JsonReader读取JArray时出错.当前的JsonReader项不是数组:StartObject

我很确定我的方法完全是错误的 - 所以如果有人能提供任何提示,我会非常感激.

Car*_*ñoz 14

也许这段代码会有所帮助:

public class Friend
{
    public string Id { get; set; }
    public string Name { get; set; }
}

...

public ActionResult About()
{
    var app = new FacebookApp();
    var result = (JsonObject)app.Get("me/friends"));
    var model = new List<Friend>();

    foreach( var friend in (JsonArray)result["data"])
        model.Add( new Friend()
        {
            Id = (string)(((JsonObject)friend)["id"]),
            Name = (string)(((JsonObject)friend)["name"])
        };

    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

现在你的模型将是类型 List<Friend>