【问题标题】:Serve JSON Data from Github Pages从 Github 页面提供 JSON 数据
【发布时间】:2017-01-05 01:25:22
【问题描述】:

我在 github 上有一个存储库,其中包含存储库根目录中的许多 csv、json、yml 数据文件。

如何让它使用 Github Pages 提供这些文件?

例如:当我转到http://username.github.io/reponame/ 时,然后是

  • /posts - 提供 posts.json 文件的内容
  • /users - 提供 users.json 文件的内容
  • /comments - 提供 cmets.json 文件的内容
  • /posts.csv - 提供 posts.csv 文件的内容

【问题讨论】:

    标签: json api github repository github-pages


    【解决方案1】:

    您只需要在您的存储库中添加.json 文件,您就可以像普通的 JSON API 一样访问它们

    在仓库username.github.io/reponame添加以下文件

    posts.json

    [
      {"id": 1, "title": "Title 1"},
      {"id": 2, "title": "Title 2"}
    ]
    

    users.json

    [
      {"name": "abc", "email": "abc@example.com"},
      {"name": "xyz", "email": "xyz@example.com"}
    ]
    

    cmets.json

    [
      {"post_id": "1", "text": "Comment 1"},
      {"post_id": "2", "text": "Comment 2"}
    ]
    

    posts.csv

    id,title
    1,Title 1
    2,Title 2
    

    并通过

    访问它们
    • http://username.github.io/reponame/posts.json
    • http://username.github.io/reponame/users.json
    • http://username.github.io/reponame/comments.json
    • http://username.github.io/reponame/posts.csv

    工作示例 -

    【讨论】:

    • 我这样做了,但是获取大约 10k 个条目需要很长时间,有没有更快的方法?
    【解决方案2】:

    您可以创建index.json 而不是index.html 来获取application/json; charset=utf-8 标头。

    也就是说,创建: - posts/index.json 将在 http://username.github.io/reponame/posts/ 服务, - users/index.json 将在 http://username.github.io/reponame/users/ 服务, 等等

    请注意,作为目录,不带斜杠 (http://username.github.io/reponame/posts) 的访问会返回 301 重定向到带有斜杠的相同路径。也就是说,它可以工作,但您的客户端需要遵循重定向(curl 默认情况下不会,curl --location 会)并且由于额外的往返行程而稍微慢一些...

    查看https://github.com/cben/sandbox/tree/master/json 目录中的工作示例(在https://cben.github.io/sandbox/json/ 提供服务)。

    附:避免在一个目录中使用多个 index.*README* 文件;当我在 index.json 旁边添加一个 README.md 时,README 获胜并在json/ 上提供服务,因此我不得不将其重命名为其他名称。

    【讨论】:

    • 如果您不想在 url 中使用 'json' 扩展名,但仍然是 applicatio/json 标头,这是唯一可行的解​​决方案。 ...谢谢!
    • 我应该添加,我有一个同名的文件和目录,例如 /json.json 和一个带有索引文件的目录,例如 /json/index.json ,直到我删除了 json.json file 该文件在目录 /json/ 上不可路由
    【解决方案3】:

    我正在添加一个关于如何从 github 使用 json 的代码块:

    function logResults(json) {
        console.log(json)
    }
    
    $.ajax({
        url: "https://raw.githubusercontent.com/cben/sandbox/master/json/index.json",
        dataType: "json"
    }).done(function(result){
        console.log(result);
    });
    

    请查看 jfiddle 上的示例: https://jsfiddle.net/v_alishauskaite/dxm8cvkL/1/

    【讨论】:

    • 您是否打算从 GitHub 而不是 GitHub 的页面中获取 json?...如果我错了,请纠正我,但我很确定这就是 raw.githubusercontent 的来源, GitHub 本身,在这种情况下来自您的 master 分支。
    • 是的,这正是它的作用。 :) 我出于好奇添加了这个。不是每个人都知道存在这样的机会。
    • 这个速率限制是多少?
    【解决方案4】:

    这种类型的 url(例如:/posts)仅适用于 html 文件。您可以将您的 json 文件命名为 posts.html 并将其前面的内容设置为:

    ---
    layout: null
    permalink: /posts/
    ---
    { "variable": "value" }
    

    然后您将在 /posts/posts/ 访问您的文件。

    唯一的缺点是返回的文件是 /posts/index.html,它使用Content-Type: text/html mime 类型而不是预期的application/json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2013-03-13
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      相关资源
      最近更新 更多