如何正确迭代 JSON 对象?

How to iterate JSON object properly?

我从 AJAX 调用中得到一个 JSON 对象,我需要从中获取所有 "name" 值。

我尝试过以多种不同的方式对其进行迭代,但均未成功。 我之前已经设法迭代 JSON 个对象,但现在值在 pkg 里面,它在 data: 里面,这就是我得到的地方麻烦。下面的代码不起作用,我不知道如何进入 pkg。

for (var key in jsonData) {
    if (jsonData.hasOwnProperty(key)) {

    }
    jQuery('#result').html(jsonData[key].join("<br/>"));
}

(jsonData.pkg) 或 (jsonData.data.pkg) 均未成功。

下面是 jsonData 对象中的值。

{
    "data": {
        "pkg": [{
            "name": "ThisIsName1",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "1",
            "MAXPOP": "unlimited"
        }, {
            "name": "ThisIsName2",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "0",
            "MAXPOP": "unlimited"
        }, {
            "name": "ThisIsName3",
            "FRONTPAGE": "n",
            "IP": "n",
            "MAXSQL": "0",
            "MAXPOP": "unlimited"
        }]
    },
    "metadata": {
        "version": 1,
        "reason": "OK",
        "result": 1,
        "command": "listpkgs"
    }
}

我在论坛中搜索了答案,但没有找到将值嵌套在对象中的人。

    for(i=0;i<jsonObj.data.pkg.length;i++){

alert(jsonObj.data.pkg[i].name;


}

试试这个代码。确保您已解析 json 字符串。并使用数组映射函数获取所有名称。

var jsonString = '{"data":{"pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},"metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}}';

var jsonData = JSON.parse(jsonString);
var names = jsonData.data.pkg.map(function(pkg){ return pkg.name }).join("<br/>");
jQuery('#result').html(names);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>

Array.prototype.map.call( jsonData.data.pkg ,function( el,i ) {

/*
Do with el whatever you want, here it will be object like -> {"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"}
*/

});

您可以尝试 3 个选项之一

var myObject = {"data":
{
    "pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},
    "metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}
}

console.log("option a:");
for (a of myObject.data.pkg) console.log(a.name);

console.log("option b:");
for (a in myObject.data.pkg) console.log(myObject.data.pkg[a].name);

console.log("option c:");
myObject.data.pkg.forEach(function (element) { console.log(element.name); });

如果您想遍历 JSON 对象并获取其所有键,我提供此函数。

请注意它无法在旧浏览器中运行...

    var json = {"data":
{"pkg":[{"name":"ThisIsName1","FRONTPAGE":"n","IP":"n","MAXSQL":"1","MAXPOP":"unlimited"},
{"name":"ThisIsName2","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"},
{"name":"ThisIsName3","FRONTPAGE":"n","IP":"n","MAXSQL":"0","MAXPOP":"unlimited"}]},
"metadata":{"version":1,"reason":"OK","result":1,"command":"listpkgs"}};


function getAllChildren(json, root) {

    function getChildren(json) {
        var result = [];
        for (var key in json) {
            result.push(key);
        }
        return result;
    }

    function isObject(json) {
        return typeof json == "object";
    }

    root = root || "";
    if (root.length > 0) {
        root += ".";
    }
    var result = [];
    var childs = getChildren(json);
    for (var i = 0; i < childs.length; i++) {
        result.push(root + childs[i]);
        if (isObject(json[childs[i]])) {
            var subchilds = getAllChildren(json[childs[i]], root + childs[i]);
            for (var j = 0; j < subchilds.length; j++) {
                result.push(subchilds[j]);
            }            
        }
    }

    return result;
}

getAllChildren(json);

它returns:

["data",
"data.pkg",
"data.pkg.0",
"data.pkg.0.name",
"data.pkg.0.FRONTPAGE",
"data.pkg.0.IP",
"data.pkg.0.MAXSQL",
"data.pkg.0.MAXPOP",
"data.pkg.1",
"data.pkg.1.name",
"data.pkg.1.FRONTPAGE",
"data.pkg.1.IP",
"data.pkg.1.MAXSQL",
"data.pkg.1.MAXPOP",
"data.pkg.2",
"data.pkg.2.name",
"data.pkg.2.FRONTPAGE",
"data.pkg.2.IP",
"data.pkg.2.MAXSQL",
"data.pkg.2.MAXPOP",
"metadata",
"metadata.version",
"metadata.reason",
"metadata.result",
"metadata.command"]