我有一个JSON对象有三个级别嵌套的我不得不遍历它动态如何在nodejs中迭代Json的嵌套属性?
这里是我的代码
var responseObj ={
db_config:{
db_user: 'das1234',
},
env_con:{
db_con:'ds67'
},
db_password: 'ds345ty76',
db_host: 'wdsa12'
}
function decrypt(responseObj,key){
var list = []
//get the keys from the responseObj
Object.keys(responseObj).forEach(function(key){
//Push the keys into a list
list.push(key);
})
console.log(list)
try{
for(var i =0;i<list.length;i++){
//Decrypt the values of the key
var decipher = crypto.createDecipher('aes256', key);
//Assign The decrypted value to the keys
responseObj[list[i]] = decipher.update(responseObj[list[i]], 'hex', 'utf8') + decipher.final('utf8')
}
return responseObj;
}catch(err){
console.log(err);
}
}
var res = decrypt(responseObj,key)
console.log(res)
试过很多方法,我只是困惑如何拿到钥匙和值动态迭代而不使用静态密钥。 有任何想法请帮助找出答案。
* “我怎么通过的Json的NodeJS嵌套循环特性?” *你不知道。 JSON是用于数据交换的*文本符号*(http://stackoverflow.com/a/2904181/157247)如果您正在处理JavaScript源代码,并且不处理*字符串*,那么您并未处理与JSON。 [(更多。)]当你解析*时,你可以遍历你所得到的对象树的嵌套属性,但是,在这一点上,你不再处理JSON。但是问题中根本没有JSON,所以不需要解析。 –
您已经获取了键并使用它们查找对象上的属性。你卡在哪里?您似乎有以上的基本工作。 –
克罗德你试图获得JSON的解决方案,这种形式的时候是对的,我能 这里是JSON的 让responseObj = { DB_USER: 'das1234', db_con: 'DS67', DB_PASSWORD: 'ds345ty76' , db_host:'wdsa12', } 但是,如果Json是嵌套的我可以如何继续下去? – Ramyachinna