且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编写脚本来编辑JSON文件?

更新时间:2023-12-04 20:05:16

这是一个完整的程序,用JavaScript(使用node.js),做你想做的事: / p>

Here's a complete program, in JavaScript (using node.js), doing what you want :

fs = require('fs');
var m = JSON.parse(fs.readFileSync('people.json').toString());
m.forEach(function(p){
    p.pic = p.name.toLowerCase()+".png";
});
fs.writeFile('people.json', JSON.stringify(m));

作为奖励(包括其他语言的其他回答者),这里有一个固定的输入JSON:

And as a bonus (including for other answerers with other languages), here's a fixed input JSON :

[
    {"name":"Paul","age":29},
    {"name":"Kathy","age":101},
    {"name":"Paula","age":12},
    {"name":"Bruce","age":56}
]