0

我尝试使用 Facebook C# SDK 页面中的 Windows Phone 示例来解决这个问题,但没有成功。

这是主要代码:

private void GetPages()
    {
        var fb = new FacebookClient(_accessToken);

        fb.GetCompleted += (o, e) =>
        {
            if (e.Error != null)
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
                return;
            }

            var result = (IDictionary<string, object>)e.GetResultData();
            // returns data and paging from Facebook

            Dispatcher.BeginInvoke(() =>
            {
                foreach (var item in result)
                {
                    // Not sure if/how to use the custom classes here
                    //item has .Key and .Value
                    //.Key = data and .Value contains the key/value pais for each of the pages returned
                }

            });
        };

        fb.GetAsync("me/accounts");
    }

// 自定义类

public class FacebookPageCollection
    {
        [JsonProperty(PropertyName = "data")]
        public FacebookPage[] data { get; set; }
        [JsonProperty(PropertyName = "paging")]
        public FacebookPagePaging paging { get; set; }
    }

    public class FacebookPage
    {
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "access_token")]
        public string AccessToken { get; set; }

        [JsonProperty(PropertyName = "category")]
        public string Category { get; set; }

        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
    }

    public class FacebookPagePaging
    {
        [JsonProperty(PropertyName = "previous")]
        public Uri previous { get; set; }

        [JsonProperty(PropertyName = "next")]
        public Uri next { get; set; }
    }

这是变量“result”返回的内容: {"data":[{"name":"value1","access_token":"value2","category":"value3","id":"value4"," perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]},{"name":"value1","access_token":"value2","category" :"value3","id":"value4","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]}],"paging":{ “下一个”:“网址”}}

我想做的是检索并保存每个页面的详细信息。

我一直试图弄清楚这一点,并查看了这里和其他地方的许多其他帖子。我只是没有足够的经验来弄清楚。

任何帮助表示赞赏。

谢谢你。斯里兰卡

4

1 回答 1

1

这是了解如何在 fb c# sdk 中使用 json 响应的技巧。

这是 Javascript JSON 和 C# JSON 之间的映射。(请注意,没有 DateTime 和另一个复杂的 .net 对象,因为它不是 JSON.org 中的 JSON 规范的一部分)

JsonObject => keyvalue pairs => IDictionary<string, object> / IDictinary<string, dynamic>
JsonArray => array => IList<object> / IList<dynamic>
string => string
number => long/decimal
boolean => bool

这是您进行实际映射的方法。

var result = (IDictionary<string, object>)e.GetResultData();
var data = (IList<object>)result["data"];
foreach(var act in data) {
    var account = (IDictionary<string,object>) act;
    var name = (string)account["name"];
    var accessToken = (string)account["access_token"];
    var id = (string)account["id"];

    // normalize to IList<string> permissions, so it is easier to work without casting.
    var permissions = ((IList<object>)account["perms"]).Select(x => (string)x).ToList();
}
于 2012-10-07T21:06:06.743 回答