简体   繁体   English

将JSON导出到JSON文件

[英]Export json to json file

I am trying to export a json to file using html and js. 我正在尝试使用html和js将json导出到文件。

This is my code: 这是我的代码:

<form onsubmit="return make_json(this);">
    name: <input type="text" name="first"/><br>
    <input type="submit" value="JSON"/>
</form>

<pre id="output">
    your json here
</pre>

<script>
    function make_json(form) {
        var json={
            "first": form.first.value
        };

        var str = JSON.stringify(json, 0, 4);
        document.getElementById('output').innerHTML = str;

        return false;
    }
</script>

As you can see, I displayed that json to the main page in html, but I have no idea how to export it to a file. 如您所见,我将json显示到html的主页中,但是我不知道如何将其导出到文件中。 I searched for an answer for a long time, but nothing helped me or mostly I couldn't understand the answer. 我搜索了很长时间的答案,但是没有任何帮助,或者大部分我无法理解答案。 Can anybody clarify this to me? 有人可以向我澄清吗?

I think you want to download a file with the json as content right? 我认为您想下载以json为内容的文件,对吗?

FileSaver.js makes this easy: https://github.com/eligrey/FileSaver.js/ FileSaver.js使此操作变得容易: https : //github.com/eligrey/FileSaver.js/

 var json = "YOU JSON"; var blob = new Blob([json], {type:"application/json;charset=utf-8"}); FileSaver.saveAs(blob, "export.sjon"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM