且构网

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

Nodejs - 通过 JSON 循环查找和替换字符串

更新时间:2023-01-16 14:07:50

你能不能简单地做一些类似的事情:

Could you not simply do something like:

fs.readFile('content.xml', 'utf-8', function (err, data){
if(err) console.log(err);

parseString(data, function(err, result){
    if(err) console.log(err);

    var json = result;
    console.log(json);

    var needle = 'This text must change';

    // Convert the result into a string, replace all instances of
    // needle with some text, then parse the json string back into
    // an object
    json = JSON.parse(JSON.stringify(result).replace(needle, SOME_REPLACEMENT_TEXT_HERE));

    ...

});