我正在尝试将github原始数据转换为JSON,但不幸的是我无法做到这一点。我正在使用reactJs我的代码:
fetch("https://api.github.com/repos/graphql-compose/graphql-compose-examples/contents/examples/northwind/data/csv/employees.csv")
.then((res) => {
if (res.ok) {
return res.content; // need to convert this content to json
} else {
throw new Error("Something went wrong");
}
})
.then((data) => console.log(data))
.catch((error) => {
console.log(error);
});发布于 2021-11-14 15:03:35
您可以在响应上调用json()。参考“基本获取请求”示例here。
content属性包含一个base64编码值,但它也包含换行符,在使用atob进行转换之前必须删除换行符。然后,您将获得CSV行:
fetch("https://api.github.com/repos/graphql-compose/graphql-compose-examples/contents/examples/northwind/data/csv/employees.csv")
.then(res => res.json())
.then(data => console.log(atob(data.content.replace('\n', ''))))输出:
employeeID,lastName,firstName,title,titleOfCourtesy,birthDate,hireDate,address,city,region,postalCode,country,homePhone,extension,photo,notes,reportsTo,photoPath
1,Davolio,Nancy,Sales Representative,Ms.,1948-12-08 00:00:00.000,1992-05-01 00:00:00.000,507 20th Ave. E. Apt. 2A,Seattle,WA,98122,USA,(206) 555-9857,5467,0x151C2F00020000000D000E0014002100FFFFFFFF4269746D617020496D616765005061696E742E506963747572650001050000020000000700000050427275736800000000000000000020540000424D20540000000000007600000028000000C0000000DF0000000100040000000000A0530000CE0E0000D80E0000000000,Education includes a BA in psychology from Colorado State University in 1970. She also completed The Art of the Cold Call. Nancy is a member of Toastmasters International.,2,http://accweb/emmployees/davolio.bmp
...https://stackoverflow.com/questions/69964248
复制相似问题