在不使用变量名的情况下打印嵌套的JSON [英] Printing nested JSON without using variable names
本文介绍了在不使用变量名的情况下打印嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Web服务返回以下嵌套的json对象:
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]);
}
但如果它是一个嵌套值,它会将值打印为[object Object],有没有办法继续深入迭代嵌套值?
however it prints value as [object Object] if it's a nested value, is there any way to keep iterating deeper into nested values ?
推荐答案
试试这个:
function iter(obj) {
for (var key in obj) {
if (typeof(obj[key]) == 'object') {
iter(obj[key]);
} else {
alert("Key: " + key + " Values: " + obj[key]);
}
}
}
BB:添加+以防止错误。
BB: added + to prevent error.
这篇关于在不使用变量名的情况下打印嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文