Question

I am using the Facebook SDK for .NET to get facebook posts from a page to display them on an app. I have the following class:

public class Posts
{
    public string id { get; set; }

    public string Name { get; set; }

    public string Message { get; set; }

    public Uri PictureUri { get; set; }
}

public class FacebookPostsData
{
    private static ObservableCollection<Posts> posts = new ObservableCollection<Posts>();

    public static ObservableCollection<Posts> Posts
    {
        get
        {
            return posts;
        }
    }
}

And the following code to get the posts:

       FacebookClient fb = new FacebookClient(App.AccessToken);

       dynamic postsTaskResult = await fb.GetTaskAsync("/mypage?fields=posts");
       var result = (IDictionary<string, object>)postsTaskResult;           
       var data = (IEnumerable<object>)result["posts"];

        foreach (var item in data)
        {
            var post = (IDictionary<string, object>)item;

            FacebookPostsData.Posts.Add(new Posts { Name = (string)post["name"], id = (string)post["id"], Message = (string)post["message"], PictureUri = new Uri(string.Format("{0}", (string)post["picture"])) });
        }

        Frame.Navigate(typeof(Pages.next));

However, result["posts"] returns a Json object and I get the following error.

Unable to cast object of type 'Facebook.JsonObject' to type 'System.Collections.Generic.IEnumerable`1[MyProject.ViewModel.Posts]'.

Any ideas on how I can convert the object?

The object is a Facebook post like the following:

 "id": "339150749455906", 
 "posts": {
  "data": [
  {
    "id": "339150749455906_545370565500589", 
    "from": {
      "category": "Food/beverages", 
      "name": "Pepsi", 
      "id": "339150749455906"
    }, 
    "story": "Pepsi updated their cover photo.", 
    "picture": "http://photos-g.ak.fbcdn.net/hphotos-ak-ash3/942740_545370555500590_46289134_s.jpg", 
    "link": "http://www.facebook.com/photo.php?fbid=545370555500590&set=a.365573920146922.72816.339150749455906&type=1&relevant_count=1", 
    "icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
    "actions": [
      {
        "name": "Comment", 
        "link": "http://www.facebook.com/339150749455906/posts/545370565500589"
      }, 
      {
        "name": "Like", 
        "link": "http://www.facebook.com/339150749455906/posts/545370565500589"
      }
    ], 
    "privacy": {
      "value": ""
    }, 
Was it helpful?

Solution

I did something like this

   FacebookClient fb = new FacebookClient(App.AccessToken);

   dynamic postsTaskResult = await fb.GetTaskAsync("/mypage?fields=posts");

    foreach (var post in postsTaskResult.posts.data)//checking for nulls here would be safer
    {
        //var post = item; //JsonObject will do fine. I have not changed it
        var name = ((IDictionary<String, object>)post).ContainsKey("name") ? (string)post.name : ""; //ContainsKey check optional
        //var name = (string)post.name; //this would do fine
        FacebookPostsData.Posts.Add(
            new Posts { 
                Name = name, 
                id = (string)post["id"], 
                Message = (string)post["message"], 
                PictureUri = new Uri(string.Format("{0}", (string)post["picture"])) });
    }

Check it out and let me know.

Disclaimer: untested code from sick-bed :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top