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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With