我正在尝试编写一个 Json 对象 (JsonExport),我想将其内容写入一个文本文件。
我正在使用 max4live 将数据从音频 DAW 导出到 Json,以便导出到服务器,但之后我想在文本文件中看到整个 Json 对象:
var txtFile = "test.txt";
var file = new File(txtFile);
var str = JSON.stringify(JsonExport);
file.open("write"); // open file with write access
file.write(str);
file.close();
编译器运行没有错误,但我无法获取文本文件。我也使用了一些目录的路径,但什么也没有。
知道发生了什么吗?谢谢
最佳答案
我知道这个问题已经被接受了答案,但我认为我的答案可以帮助别人。因此,问题是将 Json 数据导出到文本文件。执行以下代码后,浏览器将下载文件。
const filename = 'data.json';
const jsonStr = JSON.stringify(JsonExport);
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(jsonStr));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
关于javascript - 将 Json 对象导出到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780271/