Facebook的图形API检索与JSON和C#的朋友 [英] Facebook Graph API retrieve Friends with json and C#

查看:174
本文介绍了Facebook的图形API检索与JSON和C#的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#的图形API,并已能够抓住的Facebook用户的个人资料信息,如ID,姓名和电子邮件,然后反序列化JSON要能值分配给标签。

I'm working in C# with the Graph API and have been able to grab Facebook user profile information such as the ID, Name and email and then deserialize the JSON to be able to assign the values to labels.

不过,我的问题是,当我去抓住友对于这个问题清单,或任何名单,我怎么去用C#反序列化此JSON信息,以便我可以存储数据?我相信,我在寻找一种方式来反序列化的结果,一个字典对象,这样我就可以再通过数据环路。

However, my problem is when I go to grab the list of Friends, or list of anything for that matter, how do I go about deserializing this JSON information in C# so that I can store the data? I believe I'm looking for a way to deserialize the results to a dictionary object so that I can then loop through the data.

推荐答案

Well..I结束了使用JSON.Net,和它的工作太棒了。感谢您对我指着那个方向。随着另一篇文章中,我发现的帮助( http://www.mattcashatt.com )和JSON.net文件,我是能够使一切工作正常。下面是一些我使用的代码。

Well..I ended up using JSON.Net, and it worked great. Thank you for pointing me in that direction. With the help of another article I found (http://www.mattcashatt.com) and the JSON.net files, I was able to get everything working. Here is some of the code I used.

            #region JSON.Net User Profile
            //Profile URL
            url = "https://graph.facebook.com/me?fields=id,name,email&access_token=" + oAuth.Token;

            JObject myProfile = JObject.Parse(requestFBData(url));
            string myID = myProfile["id"].ToString().Replace("\"", "");
            string myName = myProfile["name"].ToString().Replace("\"", "");
            string email = myProfile["email"].ToString().Replace("\"", "");

            lblID.Text = myID;
            lblFullName.Text = myName;
            lblEmail.Text = email;
            imgUser.ImageUrl = "https://graph.facebook.com/me/picture?type=large&access_token=" + oAuth.Token;

            #endregion


            #region JSON.Net Friends

            //Friends URL
            url = "https://graph.facebook.com/me/friends?access_token=" + oAuth.Token;


            JObject myFriends = JObject.Parse(requestFBData(url));

            string id="";
            string name = "";

            //Loop through the returned friends
            foreach (var i in myFriends["data"].Children())
            {
                id = i["id"].ToString().Replace("\"", "");
                name = i["name"].ToString().Replace("\"", "");
                lblFriends.Text = lblFriends.Text + "<br/> " + "id: " + id + " name: " + name + "<img src=" + "https://graph.facebook.com/" + id + "/picture>";
            }

            #endregion



        }
    }

}

public string requestFBData(string action)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string results = sr.ReadToEnd();
    sr.Close();

    return results;
}

这篇关于Facebook的图形API检索与JSON和C#的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆