首页 > 解决方案 > github api 获取提交和提交者

问题描述

我正在尝试获取提交者列表在开始时,我要求 GitHub API (v3) 获取存储库:

https://api.github.com/users/{userName}/repos

好的,有一些列表,我得到了存储库名称,我想用它来获取所有提交

https://api.github.com/repos/{userName}/{repoName}/commits

地址是正确的,我得到了一些信息,但是......我不知道如何创建类,我不明白这个JSON结构

请参阅https://api.github.com/repos/octocat/Hello-World/git/commits/上的示例

它是这样的:

[0]
{
 "sha",
 "commit":{
       "commiter":{
        "name",
        "email",
        }
      }
}

所以......提交类应该有提交......?我尝试过这样的事情:

提交类

[DataContract(Name="commits")]
    public class Commit
    {
        [DataMember(Name="sha")]
        public string Sha{get;set;}
        [DataMember(Name="commiter")]
        public Commiter Commiter{get;set;}
    }

和提交者类:

 [DataContract(Name="commiter")]
    public class Commiter
    {
        [DataMember(Name="name")]
        public string Name{get;set;}

        [DataMember(Name="email")]
        public string Email {get;set;}

        [DataMember(Name="date")]
        public string Date{get;set;}

        [DataMember(Name="message")]
        public string Message{get;set;}
    }

但是当我要求提交时唯一的结果是来自 Commit 类的 SHA 字段,提交者始终为空

有人能帮我吗 :)?

标签: c#httpclientgithub-api

解决方案


为了生成我的 POCO 类,我总是使用:http: //json2csharp.com/

它是自动的。对于您的 API,它是: public class Author { public string name { get; 放; } 公共字符串电子邮件 { 获取;放; } 公共日期时间日期 { 获取;放; } }

public class Committer
{
    public string name { get; set; }
    public string email { get; set; }
    public DateTime date { get; set; }
}

public class Tree
{
    public string sha { get; set; }
    public string url { get; set; }
}

public class Verification
{
    public bool verified { get; set; }
    public string reason { get; set; }
    public string signature { get; set; }
    public string payload { get; set; }
}

public class Commit
{
    public Author author { get; set; }
    public Committer committer { get; set; }
    public string message { get; set; }
    public Tree tree { get; set; }
    public string url { get; set; }
    public int comment_count { get; set; }
    public Verification verification { get; set; }
}

public class Author2
{
    public string login { get; set; }
    public int id { get; set; }
    public string node_id { 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; }
}

public class Committer2
{
    public string login { get; set; }
    public int id { get; set; }
    public string node_id { 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; }
}

public class Parent
{
    public string sha { get; set; }
    public string url { get; set; }
    public string html_url { get; set; }
}

public class RootObject
{
    public string sha { get; set; }
    public string node_id { get; set; }
    public Commit commit { get; set; }
    public string url { get; set; }
    public string html_url { get; set; }
    public string comments_url { get; set; }
    public Author2 author { get; set; }
    public Committer2 committer { get; set; }
    public List<Parent> parents { get; set; }
}

推荐阅读