admin管理员组文章数量:1314471
I want the nodejs to append all the data in the JSON format in following format
[{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}, {
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
},{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}]
And I want the nodejs to append more data. I m trying append but it's appending like this
{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}
I m using the following code to append
myData= { "name": "sam" , "message": "hi how are you", "datetime": "2014-5-1 4:4:4" };
fs.appendFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if(err) {
console.log(err);
}
});
I want the nodejs to append all the data in the JSON format in following format
[{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}, {
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
},{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}]
And I want the nodejs to append more data. I m trying append but it's appending like this
{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}{
"name": "admin",
"message": "dfd",
"datetime": "2014-06-03 13:01:39"
}
I m using the following code to append
myData= { "name": "sam" , "message": "hi how are you", "datetime": "2014-5-1 4:4:4" };
fs.appendFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if(err) {
console.log(err);
}
});
Share
Improve this question
edited Jun 3, 2014 at 10:01
dave
asked Jun 3, 2014 at 9:16
davedave
1,6795 gold badges24 silver badges32 bronze badges
3
- what is code used for appending? – azero0 Commented Jun 3, 2014 at 9:33
- @A-0- question is updated – dave Commented Jun 3, 2014 at 10:01
- You might need to parse the array from the file as a javascript array, append your new data to the array and then save the array in the file. – Aliou Commented Jun 3, 2014 at 10:06
1 Answer
Reset to default 9JSON is not 'appendable' format. You have two options here:
- Read file, parse it, append data to array, serialize, replace file content.
- Switch to different file format. Actually CSV is good enough to store table-like data and is 'appendable'.
本文标签: javascriptnodejs append inside json file in array formatStack Overflow
版权声明:本文标题:javascript - nodejs append inside json file in array format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741968457a2407688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论