简体   繁体   English

遍历多维Json

[英]Loop through multidimensional Json

I have a multilevel JSon and need to loop through and get its column name and value. 我有一个多层JSon,需要遍历并获取其列名和值。

 var json   {  
       "Students":[  
          {  
             "name":{  
                "value":"Allan"
             },
             "number":{  
                "value":"123"
             }
          },
          {  
             "name":{  
                "value":"Frank"
             },
             "number":{  
                "value":"456"
             }
          }
       ]
    }

I can loop through and get the column name but cannot get the value. 我可以遍历并获取列名,但无法获取值。

var objectKeys = Object.keys(json); 
for (var key in objectKeys)
    {       
        var student = json.Students;

        for (var i = 0; i < student .length; i++) {

            for (var column in json.Students[i])
                {

                window.print(column);
                window.print(column.value);                                         

            }

        }       
}

The above gives me the following 以上给我以下

name
undefined
number
undefined
name
undefined
number
undefined

Ideally i want to treat one row separately so i can insert it in database, like insert Allan and 123 against name and number column of students table. 理想情况下,我想单独处理一行,这样我就可以将其插入数据库中,例如针对学生表的名称和数字列插入Allan和123。

var objectKeys = Object.keys(json);
for (var key in objectKeys)
{       
    var student = json.Students;

    for (var i = 0; i < student .length; i++) {

        for (var column in json.Students[i]) {
            window.print(column);
            window.print(json.Students[i][column].value);

        }

    }
}

since key in outer for loop isn't used it is redundant 由于不使用外部for循环中的key ,因此是多余的

var objectKeys = Object.keys(json); 
for (var key in objectKeys) {       
    var student = json.Students;
    for (var i = 0; i < student .length; i++) {
        window.print(json.Students[i].name.value);
        window.print(json.Students[i].number.value);

    }       
}

Your JSON object contains 1 entry with key Students and value of a list of 2 dictionaries. 您的JSON对象包含1项与主要学生和2个词典列表的价值

so var objectKeys = Object.keys(json); 所以var objectKeys = Object.keys(json); represents just one value "Students" 代表一个值“学生”

It doesn't look like you're using the var key in objectKeys as you instantiate this variable key but never use it. 实例化此可变键时 ,您似乎没有var key in objectKeys使用var key in objectKeys但是从不使用它。

Re-write your code like this if your intent is to loop through the dictionary: 如果您要遍历字典,请像这样重新编写代码:

 var json   {  
       "Students":[  
          {  
             "name":{  
                "value":"Allan"
             },
             "number":{  
                "value":"123"
             }
          },
          {  
             "name":{  
                "value":"Frank"
             },
             "number":{  
                "value":"456"
             }
          }
       ]
    }

for(let i = 0; i < json["Students"].length; i++){

    let key_values = Object.keys(json["Students"][i]);

    for(let inner_key_values in key_values){
        window.print(json["Students"][i][inner_key_values].value);
    }

}

I wrote the below snippet for you - It prints a nested object recursively. 我为您编写了以下代码段-它以递归方式打印嵌套对象。 You can modify it as you want. 您可以根据需要对其进行修改。

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3",
    "p4": {
        "p4": 'value 4'
    }
};



/**
*   Printing a nested javascript object
*/
function jsonPrinter(obj) {

    for (let key in obj) {
        // checking if it's nested
        if (obj.hasOwnProperty(key) && (typeof obj[key] === "object")) {
            jsonPrinter(obj[key])
        } else {
            // printing the flat attributes
            console.log(key + " -> " + obj[key]);
        }
    }
}

jsonPrinter(p);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM