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_ine":"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 ?
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_ine":"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 ?
Share Improve this question edited Nov 5, 2010 at 21:00 palswim 12.1k8 gold badges56 silver badges79 bronze badges asked Nov 5, 2010 at 20:55 Amr EllafyAmr Ellafy 7409 silver badges27 bronze badges6 Answers
Reset to default 7Try 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.
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]);
}
}
}
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));
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.
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);
}
}
}
I'm guessing that in your request (assuming it's Ajax) you aren't specifying the dataType to be returned as 'json'.