简体   繁体   English

Facebook SDK C# - 获取好友列表

[英]Facebook SDK C# - get friends list

Using Facebook SDK ( http://facebooksdk.codeplex.com/ ), you do something like this... 使用Facebook SDK( http://facebooksdk.codeplex.com/ ),你可以这样做......

        string myAccessToken = "something";
        FacebookClient client = new FacebookClient(myAccessToken);
        IDictionary<string, object> friendData = (IDictionary<string, object>)client.Get("/me/friends");

Now how do you get the friends data out from the dictionary? 现在如何从字典中获取朋友数据?

Your function for storing friends list from json data: 您从json数据存储朋友列表的功能:

string myAccessToken = "something";         
FacebookClient client = new FacebookClient(myAccessToken);         

var friendListData = client.Get("/me/friends");
JObject friendListJson = JObject.Parse(friendListData.ToString()); 

List<FbUser> fbUsers = new List<FbUser>();
foreach (var friend in friendListJson["data"].Children())             
{   
    FbUser fbUser = new FbUser();
    fbUser.Id = friend["id"].ToString().Replace("\"", "");                 
    fbUser.Name = friend["name"].ToString().Replace("\"", "");                 
    fbUsers.Add(fbUser);
}

Class for facebook user facebook用户的类

Class FbUser {
    String Id { set; get; }
    String Name { set; get; }
}

As I had the same issue and was looking a nice solution. 因为我有同样的问题,并且正在寻找一个很好的解决方案。 I did the same thing with linq resulting in less code. 我用linq做了同样的事情,导致代码减少。 And most of the time less is more :) 大多数时候少了更多:)

FacebookClient client = new FacebookClient(accessToken);
dynamic friendListData = client.Get("/me/friends");
var result = (from i in (IEnumerable<dynamic>)friendListData.data
             select new
             {
                 i.name,
                 i.id
             }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM