我有以下的JSON对象:如何使用JavaScript .forEach得到迭代JSON对象和JSON数组
var json = {"tsn": {
"settings":
{"app_name": "TSN", "version": "1.0"},
"occurrences":
["Party", "Music"]
}
};
我真的不明白,为什么我不能像这样访问它的值:
json.tsn.forEach(function(item){
console.log(item.settings.app_name);
console.log(item.occurrences);
});
我得到json.tsn.forEach is not a function
。
因为'forEach'是数组方法,而'json.tsn'是一个不是数组的对象 – charlietfl
你真的需要一个循环吗?或者只是'console.log(json.tsn.settings.app_name)'? – charlietfl
是的,我确实需要一个循环,因为这是一个具有更多数据的API的输出。 – Nicero