我需要使用JSON.stringify
将对象转换为字符串,然后将JSON数据保存到一个.json
文件中。
这是我的当前代码:
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);
注意:我希望能够动态保存这些数据,而我之前使用的是jsonTypescript2的jsonConverter。
我尝试了以下方法:
let a = document.createElement('a');
// JSON.stringify(jsonObject), 'ex.json' // another
a.setAttribute('href', 'data:application/json;charset=UTF-8,' + encodeURIComponent(myData));