我从API中获取数据以显示销售和财务报告,但我收到了一个类型为gzip的文件,我设法将其转换为Uint8Array。我想以某种方式将其解析为一个JSON文件,我可以使用它访问数据并在前端创建图表。
我尝试使用不同的库(pako和cborg似乎是最接近用例的库),但我最终得到了一个错误Error: CBOR decode error: unexpected character at position 0
这是我目前掌握的代码:
let req = https.request(options, function (res) {
console.log("Header: " + JSON.stringify(res.headers));
res.setEncoding("utf8");
res.on("data", function (body) {
const deflatedBody = pako.deflate(body);
console.log("DEFLATED DATA -----> ", typeof deflatedBody, deflatedBody);
console.log(decode(deflatedBody));
});
res.on("error", function (error) {
console.log("connection could not be made " + error.message);
});
});
req.end();
};
我希望有人已经发现了这一点,并有了一些想法。 非常感谢
请访问此答案https://stackoverflow.com/a/12776856/16315663以从响应中检索GZIP数据
假设您已经以UInt8Array的形式检索到完整数据
您只需要将UInt8Array作为字符串
编辑
这是对我有用的
谢谢,我实际上刚刚尝试过这种方法,但我得到了以下错误:
SyntaxError:JSON分析错误:意外标识符“x”
但我使用以下功能以文本格式打印数据:
现在我需要将其传递到JSON对象中
相关问题 更多 >
编程相关推荐