admin管理员组文章数量:1414893
let file = new Blob([response.data], {type: 'application/json'});
response.data
contains JSON file, I want to read the contents of this file and assign them as a JSON string to a variable in javascript.
I've tried a few things with FileReader.readAsText(file)
but I'm not able to properly convert it into a simple JSON string.
P.S.: The response.data
is an arraybuffer, so if there's a direct conversion that's possible without creating a Blob object, that should also solve the problem.
let file = new Blob([response.data], {type: 'application/json'});
response.data
contains JSON file, I want to read the contents of this file and assign them as a JSON string to a variable in javascript.
I've tried a few things with FileReader.readAsText(file)
but I'm not able to properly convert it into a simple JSON string.
P.S.: The response.data
is an arraybuffer, so if there's a direct conversion that's possible without creating a Blob object, that should also solve the problem.
- try to create a new text file and add data – brk Commented May 3, 2022 at 7:13
-
Use
await file.text()
orfile.text().then(jsonString => ...)
– evolutionxbox Commented May 3, 2022 at 7:17 - 1 It appears that you are trying to process a fetch response. And if is a json file then why not use Response.json? Or if you really want an array then use Response.arrayBuffer – Yogi Commented May 3, 2022 at 7:44
1 Answer
Reset to default 4let file = new Blob([response.data], {type: 'application/json'});
file.text()
.then(value => {
self.objectName = JSON.parse(value);
console.log(self.objectName);
})
.catch(error => {
console.log("Something went wrong" + error);
});
Did the trick for me.
本文标签: How can I read a Blob object in Javascript as JSON objectStack Overflow
版权声明:本文标题:How can I read a Blob object in Javascript as JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745183965a2646595.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论