![](/img/trans.png)
[英]Parse Nested json Array into Separate json Objects with Node.js
[英]Access nested json objects in node.js
我正在尝试访问嵌套的JSON
对象,但是却Cannot read property 'module' of undefined
。
这是JSON
文件。
{
"server": {
"module": {
"InPluginPath": "/usr/home/nah/Website/server/httpModule.js"
}
}
}
然后,当我使用fs.readFile()
读取文件后尝试访问JSON
对象时,出现Cannot read property 'module' of undefined
,错误。 下面是导致错误的行。
console.log(config.server.module.InPluginPath);
您需要JSON.parse()
从fs.readFile()
得到的字符串 。 例如:
fs.readFile('/tmp/foo.json', { encoding: 'utf8' }, function(err, data) {
if (err) throw err;
try {
data = JSON.parse(data);
} catch (ex) {
console.log('Error parsing json');
return;
}
console.log(data.server.module.InPluginPath);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.