GitHub API JSON包含新行(\ n)

时间:2017-11-17 15:51:20

标签: json github newline github-api

我正在使用GitHub API访问某个文件的内容,并且我在JSON密钥\n的值内得到content。为什么那些\n在那里?

content是b64编码的,但显然那些\n解码失败。我可以编写一些代码来删除\n但是他们在那里的原因是什么?

示例:https://api.github.com/repos/grantae/mips32r1_xum/contents/README

{
  "name": "README",
  "path": "README",
  "sha": "2d77ad7f50cc6843ea06b0b48d6935c57b7b5577",
  "size": 3997,
  "url": "https://api.github.com/repos/grantae/mips32r1_xum/contents/README?ref=master",
  "html_url": "https://github.com/grantae/mips32r1_xum/blob/master/README",
  "git_url": "https://api.github.com/repos/grantae/mips32r1_xum/git/blobs/2d77ad7f50cc6843ea06b0b48d6935c57b7b5577",
  "download_url": "https://raw.githubusercontent.com/grantae/mips32r1_xum/master/README",
  "type": "file",
  "content": "TUlQUzMyUjEgLSAxIEphbiAyMDE0DQotLS0tLS0tLS0tLS0tLS0tLS0tLS0N\nCg0KVGhpcyBpcyBhbiBpbXBsZW1lbnRhdGlvbiBvZiB0aGUgTUlQUzMyIFJl\nbGVhc2UgMSBwcm9jZXNzb3IuDQoNClRoaXMgZGVzaWduIHdhcyBjcmVhdGVk\nIGJ5IEdyYW50IEF5ZXJzIGFuZCBmdW5kZWQgYnkgdGhlIA0KZVh0ZW5zaWJs\nZSBVdGFoIE11bHRpY29yZSAoWFVNKSBwcm9qZWN0IGF0IHRoZSBVbml2ZXJz\naXR5IG9mIFV0YWgNCmZyb20gMjAxMC0yMDEyLiBJdCB3YXMgcHJldmlvdXNs\neSBob3N0ZWQgb24gT3BlbkNvcmVzIGFzICdtaXBzMzJyMScuDQpGZWVsIGZy\nZWUgdG8gc2VuZCBxdWVzdGlvbnMgb3IgZmVlZGJhY2sgdG8gYXllcnMgQVQg\nY3Muc3RhbmZvcmQuZWR1Lg0KDQoNClByb2Nlc3NvciBEZXRhaWxzDQotLS0t\nLS0tLS0tLS0tLS0tLQ0KDQotIFNpbmdsZS1pc3N1ZSBp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IGZvciBtb3JlDQogICAgICAgICAgICAgICAgICAg\nICAgICAgICAgaW5mb3JtYXRpb24uDQogICANCiAgIFNvZnR3YXJlOg0KICAg\nICAgLT4gZGVtb3M6ICAgICAgICAgICAgIFNvZnR3YXJlIGRlbW9zIGZvciBY\nVU0NCiAgICAgIC0+IHRvb2xjaGFpbjogICAgICAgICBJbnN0cnVjdGlvbnMg\nZm9yIGNyZWF0aW5nIGEgTUlQUyBjcm9zc2NvbXBpbGVyIHRvb2xjaGFpbg0K\nICAgICAgLT4geHVtX2Jvb3Rsb2FkZXI6ICAgIEEgQyMgaW1wbGVtZW50YXRp\nb24gb2YgdGhlIFhVTSBib290bG9hZGVyLg0KDQoNCkxlZ2FsDQotLS0tLQ0K\nDQpBbGwgc29mdHdhcmUgYW5kIGhhcmR3YXJlIGluY2x1ZGVkIGluIHRoaXMg\nZGlzdHJpYnV0aW9uIGlzIGxpY2Vuc2VkIHVuZGVyDQp0aGUgR05VIExHUEwu\nIFNlZSB0aGUgZmlsZSAnTEVHQUwnIGluIHRoaXMgc2FtZSBkaXJlY3Rvcnkg\nZm9yIG1vcmUgaW5mb3JtYXRpb24uDQoNCg0KRW5qb3khDQoNCg==\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/grantae/mips32r1_xum/contents/README?ref=master",
    "git": "https://api.github.com/repos/grantae/mips32r1_xum/git/blobs/2d77ad7f50cc6843ea06b0b48d6935c57b7b5577",
    "html": "https://github.com/grantae/mips32r1_xum/blob/master/README"
  }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用其他base64解码库来处理\n

var base64 = require('js-base64').Base64;
var contents = base64.decode(res.content);

它的工作方式与GitHub API调用头相同:

'accept': 'application/vnd.github.VERSION.raw'