【问题标题】:Get the whole List as Json from a List in R从R中的列表中获取整个列表作为Json
【发布时间】:2019-08-13 05:33:00
【问题描述】:

你好所以我正在使用获取 github api 的请求:

commitsPublic <- GET("https://<host-name>/api/v3/search/commits?
q=is:public+org:<ORG-NAME>",
add_headers(Authorization= "token <your-Token>", Accept= 
'application/vnd.github.cloak-preview'))

commitsPublic

我得到:

156 项(总数)

 Content-Type: application/json; charset=utf-8
  Size: 291 kB
{
  "total_count": 156,
  "incomplete_results": false,
  "items": [
    {
  (I removed the Items Since they are not important)

但是当我尝试转换 Json 时:

显示非结构化和可读的原始数据

jsonRespText<-content(commitsPublic,as="text") 
jsonRespText

转换为 Json:

toJson <- fromJSON(jsonRespText)
toJson

返回:

$items[[30]]$score
[1] 1

它返回一个列表,其中包含最多 30 个项目" items[[30]]

并且 items[[31]] 为 NULL

所以我要问的是如何从 Json 文本中获取所有列表中的 156 个列出的项目。我有另一个获取请求,它给了我 10.000 Total Commits。但是当我从 Json 转换时,列表仍然有 30 个。所以任何帮助都将不胜感激!

【问题讨论】:

    标签: r list get fromjson


    【解决方案1】:

    Github API 分页到 30 个结果。分页信息在响应链接头中。

    library(httr)
    
    commitsPublic <- GET("https://api.github.com/search/commits?q=is:public+org:rstudio",
                         add_headers(Accept = 'application/vnd.github.cloak-preview'))
    
    headers(commitsPublic)$link
    #> [1] "<https://api.github.com/search/commits?q=is%3Apublic+org%3Arstudio&page=2>; 
    #> rel=\"next\", 
    #> <https://api.github.com/search/commits?q=is%3Apublic+org%3Arstudio&page=34>; 
    #> rel=\"last\""
    

    reprex package (v0.2.1) 于 2019-03-22 创建

    这告诉我们下一页的位置,在这个实例中总共有 34 页。

    参考:https://developer.github.com/v3/guides/traversing-with-pagination/

    【讨论】:

    • 我明白了。但我有一个总计数为 100.000 的 git 请求。我必须喜欢这样做 300 次?
    • 基本上,是的。还有一个per_page=参数可以增加到100。
    • 您可能想看看gh package,其gh() 函数有一个.limit 参数,可以设置为Inf 并为您执行此操作
    • 每页有效,但对于 100k,我需要执行 x1000 :D。你知道 gh() 函数是如何工作的吗?
    • 是的,我正在使用 Github Enterprise。我是本组织的管理员。我正在做一个分析大学学生提交的项目。但提交来自 330 个存储库和许多学生,提交数量约为 10 万。那么 search/org:orgname/commits 是我寻找提交的正确方式吗?或者是否有其他方法可以获取组织内所有存储库的所有提交。当然,要将它们全部作为一个大清单。最后我必须将 data.table 转换为 CSV。来分析一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2019-01-15
    • 1970-01-01
    • 2015-04-03
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多