4

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.

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.

zengr
  • 38,346
  • 37
  • 130
  • 192
user402880
  • 151
  • 1
  • 1
  • 8
  • You could use a JSON parser as @zengr mentioned below, but really - most JSON deserialization can be done with the built in .NET deseriazlier - DataContractJsonSerializer. You just need the correct attributes (DataMember) on the class your trying to deserialize into. Perhaps you can paste what the JSON looks like? Maybe look at this SO question: http://stackoverflow.com/questions/596271/deserialization-problem-with-datacontractjsonserializer – RPM1984 Jul 27 '10 at 06:39
  • I'm currently using the .NET deserializer for non-list type fields, such as userid and name. I would like to be able to stick with that if possible. Here is an example of the Friends listing I'm trying to read in. {"data":[{"id":"123456","name":"barney rubble"},{"id":"987654","name":"wilma flintstone"},{"id":"123654","name":"fred flintstone"}]} – user402880 Jul 27 '10 at 11:24

2 Answers2

10

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;
}
user402880
  • 151
  • 1
  • 1
  • 8
1

You need a JSON parser for C#.

This might help: parsing JSon using JSon.net

There are many other C# JSON parsers:

  1. JSON Checker

  2. JAYROCK

More here

Community
  • 1
  • 1
zengr
  • 38,346
  • 37
  • 130
  • 192