js导出json文件
前言:在调试js的时候,想把某个变量保存文本地json文件,以便于分析代码执行过程。可以按下面步骤操作。1、在程序代码里创建一个保存文件的函数function saveJSON(data, filename){if(!data) {alert('保存的数据为空');return;}if(!filename)filename = 'json....
·
前言:在调试js的时候,想把某个变量保存文本地json文件,以便于分析代码执行过程。可以按下面步骤操作。
1、在程序代码里创建一个保存文件的函数
function saveJSON(data, filename){
if(!data) {
alert('保存的数据为空');
return;
}
if(!filename)
filename = 'json.json'
if(typeof data === 'object'){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
2、在chrome控制台调用该函数,或者使用界面上的某个dom元素的事件函数里调用上面的函数。
saveJSON(obj,"obj.json"),就可以导出js的object变量了。。。
更多推荐
已为社区贡献2条内容
所有评论(0)