从Github页面提供JSON数据

从Github页面提供JSON数据,json,api,github,repository,github-pages,Json,Api,Github,Repository,Github Pages,我在github上有一个存储库,它在存储库的根目录中包含许多csv、json、yml数据文件 如何使用Github页面为这些文件提供服务 例如:当我进入http://username.github.io/reponame/后跟 /posts-提供posts.json文件的内容 /users-提供users.json文件的内容 /comments-提供comments.json文件的内容 /posts.csv-提供posts.csv文件的内容 这种类型的url(例如:/posts)仅适用于htm

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

如何使用Github页面为这些文件提供服务

例如:当我进入
http://username.github.io/reponame/
后跟

  • /posts
    -提供posts.json文件的内容
  • /users
    -提供users.json文件的内容
  • /comments
    -提供comments.json文件的内容
  • /posts.csv
    -提供posts.csv文件的内容
这种类型的url(例如:/posts)仅适用于html文件。您可以将json文件命名为posts.html,并将其前端设置为:

---
layout: null
permalink: /posts/
---
{ "variable": "value" }
然后,您将通过/posts/posts/访问您的文件


唯一的缺点是返回的文件是/posts/index.html,它与
内容类型:text/html
mime类型一起提供,而不是预期的
应用程序/json
您只需在存储库中添加
.json
文件,就可以像正常的json API一样访问它们

在以下存储库中添加以下文件

posts.json

[
  {"id": 1, "title": "Title 1"},
  {"id": 2, "title": "Title 2"}
]
[
  {"name": "abc", "email": "abc@example.com"},
  {"name": "xyz", "email": "xyz@example.com"}
]
[
  {"post_id": "1", "text": "Comment 1"},
  {"post_id": "2", "text": "Comment 2"}
]
users.json

[
  {"id": 1, "title": "Title 1"},
  {"id": 2, "title": "Title 2"}
]
[
  {"name": "abc", "email": "abc@example.com"},
  {"name": "xyz", "email": "xyz@example.com"}
]
[
  {"post_id": "1", "text": "Comment 1"},
  {"post_id": "2", "text": "Comment 2"}
]
comments.json

[
  {"id": 1, "title": "Title 1"},
  {"id": 2, "title": "Title 2"}
]
[
  {"name": "abc", "email": "abc@example.com"},
  {"name": "xyz", "email": "xyz@example.com"}
]
[
  {"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

您可以创建
index.json
而不是
index.html
来获取
应用程序/json;字符集=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
这样做),并且由于额外的往返,速度稍慢一些

有关工作示例,请参见目录(提供于)


注意:避免在一个目录中包含多个
索引。*
自述文件*
文件;当我在index.json旁边添加README.md时,README赢了,并在
json/
上得到了服务,因此我不得不将其重命名为其他名称。

我正在添加一个关于如何从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);
});
请检查JFIDLE上的示例:

您是否打算从GitHub而不是GitHub的页面获取
json
?。。。如果我错了,请纠正我,但我非常确定这就是原始的。githubusercontent是从GitHub中提取的,GitHub本身,在本例中是从您的
主分支中提取的。是的,这正是它的功能。:)我加上这个是出于好奇。不是每个人都知道存在这样的机会。这方面的速率限制是多少?如果您不希望url中有“json”扩展,但仍然需要ApplicationO/json标头,那么这是唯一有效的解决方案。。。谢谢我应该补充一点,我有一个同名的文件和目录,例如/json.json和一个索引文件的目录,例如/json/index.json,在我删除了json.json文件之前,该文件无法在目录上路由/json/我这样做了,但获取大约10k个条目需要很长时间,有没有更快的方法?