0

I'm trying to store data from fb wall into database.

My *.cs code

public ActionResult GetWall()
        {
            JSONObject wallData = helper.Get("/me/feed");
            if (wallData != null)
            {
                var data = wallData.Dictionary["data"];
                List<JSONObject> wallPosts = data.Array.ToList<JSONObject>();
                ViewData["Wall"] = wallPosts;
            }

            return View("Index");
        }

Which gets posts from fb wall. And then I have an *.aspx file, which "breaks" my wallposts into pieces (objects) or whatever you like to call them.

foreach (Facebook.JSONObject wallItem in wallPosts)
        {
            string wallItemType = wallItem.Dictionary["type"].String;
                //AND SO ON...

What i'm trying to say is that I can access to elements inside fb JSON.

Is there a way i can access to the JSON elements inside *.cs file. Or is there a way I can store elements inside the *.aspx file to db?

And if its possible, I would like to know how. =)

Thanks for help

2

2 Answers 2

1

Don't use the SDK. Fetch the JSON Data using HttpWebRequest and then Deserialize it using System.Runtime.Serialization.Json.DataContractJsonSerializer. You just have to Create a class with same properties returned in JSON data. See the example below

string Response = Utilities.HttpUtility.Fetch("https://graph.facebook.com/me?access_token=" + AccessToken, "GET", string.Empty);
            using (System.IO.MemoryStream oStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(Response)))
            {
                oStream.Position = 0;
                return (Models.FacebookUser)new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Models.FacebookUser)).ReadObject(oStream);
            }
Sign up to request clarification or add additional context in comments.

2 Comments

What happens when Facebook change the JSON structure returned and it doesn't map to your class that is deployed into production land anymore?
Those properties will simply not get mapped. Even if you use a Dictionary, it might happen that the key does not exist anymore as FB removed it. By writing your own library, you at least have control over the changes and are not dependent on some 3rd party.
1

I'm not familiar with the facebook API and the question isn't too clear, but I assume that the string wallItemType is itself a JSON string, and you wish to parse it.

I'd use the Json.Net library to parse this.

using Newtonsoft.Json;

//your code as above

foreach (Facebook.JSONObject wallItem in wallPosts)
    {
        string wallItemType = wallItem.Dictionary["type"].String;
        //I assume wallItemType is a JSON string {"name":"foobar"} or similar

        JObject o = JObject.Parse(wallItemType);

        string name = (string)o["name"]; //returns 'foobar'
        //and so on

you can also use the Json.Net to deserialize to a custom type. This custom type could be mapped to a SQL database using NHibernate.

If you wish to store the entire json string in a database, then you could use a document database such as CouchDB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.