我有一個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