如何将Facebook页面feed's JSON映射到ASP中的模型?净MVC

本文关键字:ASP 映射 模型 MVC JSON Facebook 页面 feed | 更新日期: 2023-09-27 18:15:36

我是MVC的新手,很抱歉这个问题有点晦涩。我想从我的facebook页面提取的饲料,并在我的网站上的博客风格显示它。

使用[this][1]教程,我设法从Facebook页面提要获得以下JSON

{
   "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"
   }
}

这是我的模型:

  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; }
    }
}

这是控制器:

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);
        }

我试图将Json映射到模型中的属性,但我不断得到null:(null) [2] .

尝试了以下解决建议:https://stackoverflow.com/a/9988494/2955845
使用Newtonsoft(或LINQ到JSON)将JSON反序列化到。net对象
使用JSON将JSON数据反序列化为c#。净
使用Newtonsoft(或LINQ到JSON)将JSON反序列化到。net对象

似乎什么都不起作用。我如何从JSON中创建模型并将它们映射到以下布局:http://htmlstream.com/preview/unify-v1.9.6/Blog-Magazine/blog_page_layouts2.html

如何将Facebook页面feed's JSON映射到ASP中的模型?净MVC

将这一行改为

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

:

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