2017-03-16 50 views
-1

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

試過很多方法,我只是困惑如何拿到鑰匙和值動態迭代而不使用靜態密鑰。 有任何想法請幫助找出答案。

+0

* 「我怎麼通過的Json的NodeJS嵌套循環特性?」 *你不知道。 JSON是用於數據交換的*文本符號*(http://stackoverflow.com/a/2904181/157247)如果您正在處理JavaScript源代碼,並且不處理*字符串*,那麼您並未處理與JSON。 [(更多。)]當你解析*時,你可以遍歷你所得到的對象樹的嵌套屬性,但是,在這一點上,你不再處理JSON。但是問題中根本沒有JSON,所以不需要解析。 –

+0

您已經獲取了鍵並使用它們查找對象上的屬性。你卡在哪裏?您似乎有以上的基本工作。 –

+0

克羅德你試圖獲得JSON的解決方案,這種形式的時候是對的,我能 這裏是JSON的 讓responseObj = { DB_USER: 'das1234', db_con: 'DS67', DB_PASSWORD: 'ds345ty76' , db_host:'wdsa12', } 但是,如果Json是嵌套的我可以如何繼續下去? – Ramyachinna

回答

1

讓我們先澄清JSON實際是什麼。 JSON是一種文本的,不依賴語言的數據交換格式,非常類似於XML,CSV或YAML。

來源:What is the difference between JSON and Object Literal Notation?

這beeing說你的問題是關於迭代對象本身沒有要求的關鍵。您正在尋找這樣的:

for (variable in object) {... } 

這裏是MDN一個例子:

let obj = {a:1, b:2, c:3}; 

for (let prop in obj) { 
    console.log("o." + prop + " = " + obj[prop]); 
} 

來源:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for...in


旁註

我不知道是什麼你想用你的JSON結構來實現,但你是c烏爾德您的JSON從:

var responseObj ={ 
      db_config:{ 
      db_user: 'das1234', 
      }, 
      env_con:{ 
      db_con:'ds67' 
      }, 
      db_password: 'ds345ty76', 
      db_host: 'wdsa12' 
      } 

要這樣:

let responseObj = { 
      db_user: 'das1234', 
      db_con:'ds67', 
      db_password: 'ds345ty76', 
      db_host: 'wdsa12', 
      } 

新的JSON是平的,你不必擔心通過嵌套對象進行迭代。

例子:

for (let prop in responseObj) { 
    console.log("o." + prop + " = " + responseObj [prop]); 
} 
+0

,謝謝但我必須迭代我提到的Json,因爲我僅以這種形式得到響應。我嘗試使用它迭代以解密json值。 – Ramyachinna

+0

你可以在循環內進行另一個內部檢查,如:if(typeof responseObj [prop] ==='object''(typeofof要小心)然後做另一個for(對象中的變量){...} (遞歸),然後返回你想要的值,另外一個選擇是在迭代之前將給定的JSON平坦化,在這裏有許多選項。 – Megajin

1

你已經擁有幾乎所有的作品。你知道如何從對象獲得的屬性名稱(鍵):

Object.keys(obj); 

...以及如何遍歷它們

.forEach(...) 

唯一缺少的一塊是遞歸和檢測到財產的價值是另一個對象。您可以使用typeof測試taht:typeof something === "object"告訴我們something是一個對象(或null)。

看評論:

var responseObj = { 
    db_config: { 
     db_user: 'das1234', 
    }, 
    env_con: { 
     db_con: 'ds67' 
    }, 
    db_password: 'ds345ty76', 
    db_host: 'wdsa12' 
}; 

function decrypt(obj, key) { 
    // Loop through this object's properties 
    Object.keys(obj).forEach(function(key) { 
     // Get this property's value 
     var value = obj[key]; 
     // If not falsy (null, empty string, etc.)... 
     if (value) { 
      // What is it? 
      switch (typeof value) { 
       case "object": 
        // It's an object, recurse 
        decrypt(value, key); 
        break; 
       case "string": 
        // It's a string, decrypt 
        var decipher = crypto.createDecipher('aes256', key); 
        obj[key] = decipher.update(value, 'hex', 'utf8') + decipher.final('utf8'); 
        break; 
      } 
     } 
    }) 
} 

var res = decrypt(responseObj, key); 
console.log(res);