将json导出到json文件 [英] Export json to json file

查看:112
本文介绍了将json导出到json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:



pre> < form onsubmit =return make_json(this);>
名称:< input type =textname =first/>< br>
< input type =submitvalue =JSON/>
< / form>

< pre id =output>
您的json在这里
< / pre>

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

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

返回false;
}
< / script>

正如您所看到的,我将该json显示在html的主页面中,但我不知道如何将其导出到文件。我搜索了很长时间的答案,但没有任何帮助我或大部分我无法理解答案。任何人都可以向我澄清这一点吗?

解决方案

我想你想用json下载一个文件作为内容吗?



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);


I am trying to export a json to file using html and js.

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. 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?

FileSaver.js makes this easy: 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");

这篇关于将json导出到json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆