Javascript 将 Json 对象导出到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33780271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:28:37  来源:igfitidea点击:

Export a Json object to a text File

javascriptjsonfwrite

提问by Albeis

I'm trying to write a Json object (JsonExport) and I'd like to write its content into a text file.

我正在尝试编写一个 Json 对象(JsonExport),并且我想将其内容写入一个文本文件中。

I'm using max4live to export data from Audio DAW to Json in order to export to a server, but after that I would like to see the whole Json Object in a text file:

我正在使用 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();

The compiler runs with no error, but i can not get the text file. I have used as well path to some of my directories and nothing.

编译器运行没有错误,但我无法获取文本文件。我也使用了一些目录的路径,但什么也没有。

Any idea what's happening? Thanks

知道发生了什么吗?谢谢

采纳答案by Albeis

Finally I got it! It worked by changing few parameters like this:

最后我明白了!它通过改变几个参数来工作:

   var txtFile = "/tmp/test.txt";
   var file = new File(txtFile,"write");
   var str = JSON.stringify(JsonExport);

   log("opening file...");
   file.open(); 
   log("writing file..");
   file.writeline(str);
   file.close();

Path to my directories not allowed, so i had to save it on /tmp directory. Thanks to all!

不允许我的目录路径,所以我不得不将它保存在 /tmp 目录中。谢谢大家!

回答by CBarr

If you have access to an already existing file, just link to it. You can specify what the downloaded file name will be like this:

如果您有权访问已存在的文件,只需链接到该文件即可。您可以指定下载的文件名,如下所示:

<a href="path/to/file.txt" download="example.json">
    Download as JSON
</a>

If needed, you could also write out the dataURI as well

如果需要,您也可以写出 dataURI

 //Get the file contents
 var txtFile = "test.txt";
 var file = new File(txtFile);
 var str = JSON.stringify(JsonExport);

 //Save the file contents as a DataURI
 var dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(str);

 //Write it as the href for the link
 var link = document.getElementById('link').href = dataUri;

Then just give the link an ID and a default href

然后只需给链接一个 ID 和一个默认值 href

<a href="#" id="link" download="example.json">
    Download as JSON
</a>

回答by NutCracker

I know this question already has accepted answer but I think my answer could help someone. So, the problem is to export Json data to a text file. Once you execute the following code, file will be downloaded by the browser.

我知道这个问题已经接受了答案,但我认为我的答案可以帮助某人。所以,问题是将 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);