我从AJAX调用中获取一个JSON对象,我需要从中获取所有“name”值。
我试图以多种方式迭代它但没有成功。 我之前设法迭代JSON对象,但现在值在 pkg 里面,在数据里面:这就是我遇到麻烦的地方。下面的代码不起作用,我不知道如何进入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"
}
}
我在论坛上搜索了答案,但我没有发现某人将值嵌套在对象中。
答案 0 :(得分:2)
试试这段代码。确保已解析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>
答案 1 :(得分:0)
for(i=0;i<jsonObj.data.pkg.length;i++){
alert(jsonObj.data.pkg[i].name;
}
答案 2 :(得分:0)
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 :(得分:0)
您可以尝试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); });
答案 4 :(得分:0)
如果你想迭代一个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);
它返回:
["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"]