Oma*_*mar 3 json node.js typescript angular6
使用以下.json命令将对象转换为字符串后,我需要将 JSON 数据保存到文件中JSON.stringify
这是我当前的代码:
const jsonObject: object = {
'countryName': 'Switzerland',
'users': [
{
'id': 1,
'name': 'Basel',
'founded': -200,
'beautiful': true,
'data': 123,
'keywords': ['Rhine', 'River']
},
{
'id': 1,
'name': 'Zurich',
'founded': 0,
'beautiful': false,
'data': 'no',
'keywords': ['Limmat', 'Lake']
}
]
};
const myData = JSON.stringify(jsonObject);
Run Code Online (Sandbox Code Playgroud)
注意:我想动态保存这个数据,我习惯了jsonTypescript2的jsonConverter。
我尝试使用这种方法:-
let a = document.createElement('a');
// JSON.stringify(jsonObject), 'ex.json' // another
a.setAttribute('href', 'data:application/json;charset=UTF-8,' + encodeURIComponent(myData));
Run Code Online (Sandbox Code Playgroud)
好吧,6 天前我发现了最好的解决方案,如何直接和动态地连接 Angular 6 和 Node.js + json 文件。
npm install file-saver --save
import { saveAs } from 'file-saver';
const jsonObject: object = {
'City': [
{
'id': 1,
'name': 'Basel',
'founded': -200,
'beautiful': true,
'data': 123,
'keywords': ['Rhine', 'River']
},
{
'id': 1,
'name': 'Zurich',
'founded': 0,
'beautiful': false,
'data': 'no',
'keywords': ['Limmat', 'Lake']
}
]
};
const blob = new Blob([JSON.stringify(jsonObject)], {type : 'application/json'});
saveAs(blob, 'abc.json');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15211 次 |
| 最近记录: |