var permission = require('./permission.json');
for(i in permission) {
if(permission[i] == "admin")
console.log('admin role is present');
}

{
"admin": {
"user": {
"read": true,
"write": true
},
"packages": {
"read": true,
"write": true
}
}
}

我正在尝试检查json文件中的admin
角色。怎么做,有什么建议??
答案 0 :(得分:2)
由于你的json中没有数组,你所做的事情是行不通的。而不是'许可[i]'只是使用'我'。 实施例 -
var permission = require('./permission.json');
for(i in permission) {
//because 'i' already contains the value of the key
if(i == "admin")
console.log('admin role is present');
}