a web service returns the following nested json object:
{"age":"21-24","gender":"Male","location":"San Francisco, CA","influencer score":"70-79","interests":{"Entertainment":{"Celebrities":{"Megan Fox":{},"Michael Jackson":{}},},"Social Networks & Online Communities":{"Web Personalization": {},"Journals & Personal Sites": {},},"Sports":{"Basketball":{}},},"education":"Completed Graduate School","occupation":"Professional/Technical","children":"No","household_income":"75k-100k","marital_status":"Single","home_owner_status":"Rent"}
i just want to iterate through this object without specifying property name, i tried the following code :
for (var data in json_data) {
alert("Key:" + data + " Values:" + json_data[data]);
}
however it prints value as [object Object] if it's a nested value, is there any way to keep iterating deeper into nested values ?
6 Answers 6
Try this:
function iter(obj) {
for (var key in obj) {
if (typeof(obj[key]) == 'object') {
iter(obj[key]);
} else {
alert("Key: " + key + " Values: " + obj[key]);
}
}
}
BB: added + to prevent error.
1 Comment
You can do this recursively.
function alertobjectKeys(data) {
for (var key in data) {
if (typeof(data[key]) == "object" && data[key] != null) {
alertobjectKeys(data[key]);
} else {
alert("Key:" + key + " Values:" + data[key]);
}
}
}
1 Comment
This of course requires recursion
(function(obj) {
for (var key in obj) if (obj.hasOwnProperty(key)) {
if (typeof obj[key] == 'object' && obj[key] !== null)
arguments.callee(obj[key]);
else
alert("Key: " + key + " Values: " + obj[key]);
}
)(json_data));
1 Comment
function recursiveParse(variable) {
for (var key in variable) {
if ((is_object(variable[key])) || (is_array(variable[key]))) {
recursiveParse(variable[key]);
} else {
// Process variable here
}
}
}
Then just call that function with the parameter being the json data, it will parse through the remaining objects and arrays recursively.
2 Comments
You can always create a recursive function:
function alertObj(obj) {
for (var data in obj) {
if(typeof(obj[data]) === "object")
alertObj(obj[data]);
else
alert("Key:" + data + " Values:" + obj[data]);
}
}
As long as you aren't worried about recursing too far (which you probably don't need to with JSON objects).
You can also do this with a queue or stack (array) structure:
function alertObj_queue(obj) { // Breadth-first
var arrPrint = [{key:"Object", data:obj}]; // Initialize array with original object
for(var i = 0; i < arrPrint.length; i++) {
if(typeof(arrPrint[i].data) === "object") {
for(var k in arrPrint[i].data) { // Add each to end of array
arrPrint.push({key: k, data: arrPrint[i].data[k]});
}
alert("Object key: " + arrPrint[i].key);
} else {
alert("Key:" + arrPrint[i].key + " Values:" + arrPrint[i].data);
}
}
}
function alertObj_stack(obj) { // Depth-first
var arrPrint = [{key:"Object", data:obj}]; // Initialize array with original object
while(arrPrint.length) {
var o = arrPrint.pop();
if(typeof(o.data) === "object") {
for(var k in o.data) { // Add each to end of array
arrPrint.push({key: k, data: o.data[k]});
}
alert("Object key: " + o.key);
} else {
alert("Key:" + o.key + " Values:" + o.data);
}
}
}
3 Comments
alert for any of those. It would probably make things easier to have some of your JSON data as arrays ("Sports", "Celebrities", etc.).I'm guessing that in your request (assuming it's Ajax) you aren't specifying the dataType to be returned as 'json'.