【发布时间】:2015-04-27 03:06:25
【问题描述】:
所以 iv 一直在做一些进一步的研究、测试等,并得到了一些人的帮助。并且 iv 能够想出以下方法:
public void GithubLastCommit(string apiLink)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
using (var response = client.GetAsync(apiLink).Result)
{
var json = response.Content.ReadAsStringAsync().Result;
dynamic commits = JArray.Parse(json);
DateTime lastCommit = commits[0].commit.author.date;
DateTime now = DateTime.Now;
int Days = (int)((now - lastCommit).TotalDays);
int Hours = (int)((now - lastCommit).TotalHours);
int Minutes = (int)((now - lastCommit).TotalMinutes);
MessageBox.Show(Hours.ToString());
if(Hours <=24)
{
MessageBox.Show(Hours.ToString() + "Hours ago");
}
}
}
}
现在,我发送的 apiLink 只是一个随机 github,我发现要在其上进行测试:https://api.github.com/repos/Homebrew/homebrew/commits 但是我似乎没有得到正确的值(在撰写本文时 1 小时前),不管我是什么将索引更改为它没有给我任何正确的值..我可能做错了什么?
【问题讨论】:
-
你目前得到什么输出,你期望什么?我怀疑您的 JSON 解析错误,或者您的日期代码有问题。
-
我目前正在从提供的 api 链接接收整个 json 文件,并将其解析为 JArray,我在其中选择第一个索引并浏览可用的部分以达到日期。添加的每个部分或文件都有一个特定于每个部分或文件的哈希或 SHA 代码。我要查找的主要代码是 SHA 为 47146d 的那个。等等,当查看 api 链接时,它是第一个因此提交的代码[0] 然后我试图从 commit.author.date 中检索最新的提交,我期待 1 但由于某种原因得到 11。
-
发布您收到的内容和获得的内容。
-
使用 (var response = client.GetAsync(apiLink).Result) { var json = response.Content.ReadAsStringAsync().Result;有了这个..它从链接的页面中检索所有文本。粘贴评论太多了,但它是一个 json 格式的文本页面。 (它是 json 中的对象数组)所以我选择了第一个索引,它采用第一个对象并让我访问它的属性。
标签: c# json http github commit