1

I'm new to MVC, sorry for noobish question. I want to extract the feed from my facebook page and display it in a blog style on my website.

Using [this][1] tutorial, I managed to get the following JSON from Facebook page feed

{
   "data": [
      {
         "picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQBeTNJKl6E6Cn-z&w=130&h=130&url=https\u00253A\u00252F\u00252Fi.ytimg.com\u00252Fvi\u00252FBzMLA8YIgG0\u00252Fmaxresdefault.jpg&cfs=1",
         "message": "Deilig Nyhet fra Microsoft :)",
         "full_picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQCltay6XGCdIOCg&w=720&h=720&url=https\u00253A\u00252F\u00252Fi.ytimg.com\u00252Fvi\u00252FBzMLA8YIgG0\u00252Fmaxresdefault.jpg&cfs=1",
         "link": "https://youtu.be/BzMLA8YIgG0",
         "created_time": "2016-10-27T05:49:35+0000",
         "id": "560323937478408_668291570014977"
      },
      {
         "message": "KaBooM! http://fontbomb.ilex.ca/",
         "link": "http://fontbomb.ilex.ca/",
         "created_time": "2016-10-24T20:58:25+0000",
         "id": "560323937478408_666850766825724"
      },
      {
         "picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQBEIo-gZdGvA-v-&w=130&h=130&url=http\u00253A\u00252F\u00252Fubercookie.robinlinus.com\u00252Fubercookie-logo-large.png&cfs=1",
         "message": "Hvor er dere logget p\u00e5? https://robinlinus.github.io/socialmedia-leak/",
         "full_picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQCqNxZZHQsDq0c7&url=http\u00253A\u00252F\u00252Fubercookie.robinlinus.com\u00252Fubercookie-logo-large.png",
         "link": "https://robinlinus.github.io/socialmedia-leak/",
         "created_time": "2016-10-14T18:46:26+0000",
         "id": "560323937478408_660491507461650"
      }
   ],
   "paging": {
      "previous": "https://graph.facebook.com/v2.8/560323937478408/posts?fields=picture,message,full_picture,link,created_time&limit=3&since=1477547375&access_token=170251210100617|9cbeQyqZzCnUvgj_54jQgcDkhOQ&__paging_token=enc_AdAeaZAnnXTT3FMqZAqxrHyadsW4xpOuX9okNhxuIuJ8xFCUHM9sEFky1uDa8WOCIDS74TXXkQVa3qMmNvDHMsGescfvTr69uGtjznZARVTtFkiPwZDZD&__previous=1",
      "next": "https://graph.facebook.com/v2.8/560323937478408/posts?fields=picture,message,full_picture,link,created_time&limit=3&access_token=170251210100617|9cbeQyqZzCnUvgj_54jQgcDkhOQ&until=1476470786&__paging_token=enc_AdDwbf48PqgVZAO0K7b1ew9DCLX97UbPRZBhZAhAwb3dYZC3XWKLWvntEZBAxh2X1ZB4pdQc635i3ZAPtdQ7wxQ6YUIwEpb7vlwZAzSqDTBKd8ZCAtnsacgZDZD"
   }
}

Here's my model:

  public class FacebookPost
    {
        public string picture { get; set; }
        public string message { get; set; }
        public string full_picture { get; set; }
        public string link { get; set; }
        public string created_time { get; set; }
        public string id { get; set; }
    }

    public class Paging
    {
        public string previous { get; set; }
        public string next { get; set; }
    }

    public class ListOfPosts
    {
        public List<FacebookPost> Posts { get; set; }
        public Paging paging { get; set; }
    }
}

Here's the controller:

public ActionResult News()
        {
            var NumberofFeeds = 3;
            string PageId = "pageID";
            string AccessToken = "accessToken";
            //dynamic posts;
            FacebookPost posts;

            string FeedRequestUrl = string.Concat("https://graph.facebook.com/" + PageId + "/posts?fields=picture,message,full_picture,link,created_time&access_token=" + AccessToken + "&limit=" + NumberofFeeds);
            HttpWebRequest feedRequest = (HttpWebRequest)WebRequest.Create(FeedRequestUrl);
            feedRequest.Method = "GET";
            feedRequest.Accept = "application/json";
            feedRequest.ContentType = "application/json; charset=utf-8";
            WebResponse feedResponse = (HttpWebResponse)feedRequest.GetResponse();
            using (feedResponse)
            {
                using (var reader = new StreamReader(feedResponse.GetResponseStream()))
                {
                    posts = JsonConvert.DeserializeObject<FacebookPost>(reader.ReadToEnd());
                }
            }
            return View(posts);
        }

I'm trying to map Json to properties in the model, but I constantly get null: [nulls][2].

Tried the following solutions suggestions: https://stackoverflow.com/a/9988494/2955845
Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
Deserializing JSON data to C# using JSON.NET
Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

Nothing seems to work.. How could I create models out of JSON and map them to the following layout: http://htmlstream.com/preview/unify-v1.9.6/Blog-Magazine/blog_page_layouts2.html

Community
  • 1
  • 1
Visvald
  • 37
  • 4

1 Answers1

1

Change this line from:

posts = JsonConvert.DeserializeObject<FacebookPost>(reader.ReadToEnd());

to:

posts = JsonConvert.DeserializeObject<ListOfPosts>(reader.ReadToEnd());
Mahdi
  • 3,199
  • 2
  • 25
  • 35