【问题标题】:Binding Data from web service using URL and bind it with a class - ASP.NET MVC 5使用 URL 绑定来自 Web 服务的数据并将其与类绑定 - ASP.NET MVC 5
【发布时间】:2017-02-11 07:54:07
【问题描述】:

我有这个 URL https://api.github.com/users 得到一个 json 文件
这是一个示例或返回的未绑定值:

    [
  {
    "login": "mojombo",
    "id": 1,
    "avatar_url": "https://avatars.githubusercontent.com/u/1?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/mojombo",
    "html_url": "https://github.com/mojombo",
    "followers_url": "https://api.github.com/users/mojombo/followers",
    "following_url": "https://api.github.com/users/mojombo/following{/other_user}",
    "gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
    "organizations_url": "https://api.github.com/users/mojombo/orgs",
    "repos_url": "https://api.github.com/users/mojombo/repos",
    "events_url": "https://api.github.com/users/mojombo/events{/privacy}",
    "received_events_url": "https://api.github.com/users/mojombo/received_events",
    "type": "User",
    "site_admin": false
  },
  {
    "login": "defunkt",
    "id": 2,
    "avatar_url": "https://avatars.githubusercontent.com/u/2?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/defunkt",
    "html_url": "https://github.com/defunkt",
    "followers_url": "https://api.github.com/users/defunkt/followers",
    "following_url": "https://api.github.com/users/defunkt/following{/other_user}",
    "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
    "organizations_url": "https://api.github.com/users/defunkt/orgs",
    "repos_url": "https://api.github.com/users/defunkt/repos",
    "events_url": "https://api.github.com/users/defunkt/events{/privacy}",
    "received_events_url": "https://api.github.com/users/defunkt/received_events",
    "type": "User",
    "site_admin": true
  }
]

我需要将此数据绑定到一个类以在我的控制器中使用该类...任何建议

【问题讨论】:

    标签: c# json asp.net-mvc model-view-controller asp.net-mvc-5


    【解决方案1】:

    这是你的课

    public class User
    {
        public int Id { get; set; }
        public string Login { get; set; }
        public string Avatar_url { get; set; }
        public string Gravatar_id { get; set; }
        public string Url { get; set; }
        public string Html_url { get; set; }
        public string Followers_url { get; set; }
        public string Following_url { get; set; }
        public string Gists_url { get; set; }
        public string Starred_url { get; set; }
        public string Subscriptions_url { get; set; }
        public string Organizations_url { get; set; }
        public string Repos_url { get; set; }
        public string Events_url { get; set; }
        public string Received_events_url { get; set; }
        public string Type { get; set; }
        public bool Site_admin { get; set; }
    }
    

    以下是如何将数据加载到用户列表中

    using (WebClient wc = new WebClient()) 
    {
       List<User> users = JsonConvert.DeserializeObject<List<User>>(wc.DownloadString("https://api.github.com/users"));
    }
    

    【讨论】:

    • 这行代码using (WebClient wc = new WebClient()) { List&lt;User&gt; users = JsonConvert.DeserializeObject&lt;User&gt;(wc.DownloadString("https://api.github.com/users")); }在哪里写呢?
    • @AhmedHossamAlhansy 因为您的问题是关于“将接收到的数据绑定到控制器中的类”,而不是我这里唯一的答案是“在您的控制器中”。或者你想处理从api.github.com/users数据收到的任何其他地方
    • 无法将类型“GitHubData”隐式转换为“System.Collections.Generic.List
    • @AhmedHossamAlhansy 然后尝试 JsonConvert.DeserializeObject>。我也在上面修复了它
    • 谢谢@Franky_frankly
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2015-04-06
    • 1970-01-01
    • 2020-05-05
    • 2012-02-08
    相关资源
    最近更新 更多