我有一个需要转换成JSON的Javascript对象。该对象位于.js文件中。我需要读取.js文件并将对象转换为JSON。我正在尝试和NodeJS一起做这件事。下面是.js文件中的对象:
module.exports = [
'example1',
'test1',
'example2',
'tester3'
];
下面是我用来读取文件的代码:
var fs = require('fs');
var content;
fs.readFile('/someotherpath/somewhereelse/myfile.js'), function read(err, data) {
if (err) {
throw err;
}
content = data;
processFile();
});
function processFile() {
console.log(content);
}
我的结果是:
<Buffer 2f 2f 2f 4c 69 73 74 20 77 65 62 68 6f 6f 6b 20 70 61 74 68 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 70 72 6f 78 69 65 64 0a 2f 2f 20 74 6f ... >
如何将文件、对象的实际内容转换为JSON字符串?
https://stackoverflow.com/questions/51912145
复制相似问题