[英]Adding data to existing JSON file with react native
I'm trying to add some new data to an existing JSON file from text input. 我正在尝试从文本输入中将一些新数据添加到现有JSON文件中。 I use React Native and I store some data in JSON file called PartyInfo.json, when I fill out a form I want the data will be passed and stored in the PartyInfo.json file.
我使用React Native,并将一些数据存储在名为PartyInfo.json的JSON文件中,当我填写表格时,我希望数据将被传递并存储在PartyInfo.json文件中。
const PartyInfo = require('../PartyInfo.json');
let party = {
name: this.state.name,
info: this.state.info,
date: this.state.date,
price: this.state.price,
};
let data = JSON.stringify(party);
PartyInfo.writeFile('PartyInfo.json', data);
I have tried to follow some answers to similar questions but nothing worked... thanks in advance. 我已尝试对类似问题进行一些解答,但没有任何效果。
You just need to install npm i react-native-fs using npm i react-native-fs and use below code to update your file 您只需要使用npm i react-native-fs安装npm i react- native-fs并使用以下代码更新文件
var RNFS = require('react-native-fs');
var filePath = RNFS.DocumentDirectoryPath + '/YOUR_FILE_NAME';
RNFS.writeFile(filePath, YOUR_TEXT, 'utf8')
.then((success) => {
console.log('SUCCESS');
})
.catch((err) => {
console.log(err.message);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.