Reputation: 35
I get a JSON object from an AJAX call that I need to get all the "name" values from.
I have tried to iterate it in many different ways without success. I have managed to iterate JSON objects before but now the values are inside pkg which is inside data: and that is where i get trouble. code below does not work and I do not know how to get into pkg.
for (var key in jsonData) {
if (jsonData.hasOwnProperty(key)) {
}
jQuery('#result').html(jsonData[key].join("<br/>"));
}
No success with (jsonData.pkg) or (jsonData.data.pkg) either.
Underneath is the value inside jsonData object.
{
"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"
}
}
I have searched the forum for answers but I have not found someone that has the values nested inside the object.
Upvotes: 0
Views: 805
Reputation: 3578
I offer this function if you want to iterate over a JSON object and get all its keys.
Please take in account it could not work in old browsers...
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);
And it 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"]
Upvotes: 0
Reputation: 7283
You can try one of 3 options
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); });
Upvotes: 0
Reputation: 14591
Try this code. Ensure you have parsed the json string. And use array map function to get all names.
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>
Upvotes: 2
Reputation: 2238
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"}
*/
});
Upvotes: 0
Reputation: 140
for(i=0;i<jsonObj.data.pkg.length;i++){
alert(jsonObj.data.pkg[i].name;
}
Upvotes: 0