Javascript和Parsed JSON - 子节点的['@attributes']。type返回undefined
我解析了一个合法的JSON对象,我正在遍历多维数组,先找到基于函数参数的年份,然后当年的月份。Javascript和Parsed JSON - 子节点的['@attributes']。type返回undefined
我的for循环嵌套:
for (year in yearsData) {
//If we've found the correct year...
if (yearsData[year]['@attributes'].name == yearToGet) {
//...walk through that year's child months
for (month in yearsData[year]) {
//This works!
console.log(yearsData[year][month]);
//This also works!
console.log(yearsData[year][month]['@attributes']);
//This does not work! ..but this is what I need!
console.log(yearsData[year][month]['@attributes'].name);
}
}
我可以跟踪在Firebug的控制台中的对象(有效输出)一路攀升至
的console.log(yearsData [年] [月] [ '@属性']);
但一瞬间,我在末尾添加类型(.NAME):
yearsData [年] [月] [ '@属性']是不确定的。
console.log(yearsData [year] [month]);返回有效的对象,并且我可以参见列出的有效@attributes。
我检查了多次拼写;我已经将属性的名称更改为其他的东西,以防与年份的属性发生冲突......但我没有得到任何答案。
我错过了什么简单的东西?
的JSON:
{
"year": [
{
"@attributes": {
"name": "2013"
},
"month": {
"@attributes": {
"name": "January"
},
"winner": "None!",
"theme": "Nobody!"
}
}
]
}
考虑使用jQuery.grep或jQuery.each到对象上的迭代和分析有效地工作。它可以帮助很多。
对不起,我没有这个特定项目的选项。 – Kate 2013-03-05 19:24:29
一些brainbashing之后,它竟然是一个超级DERP。我使用月份变量而不是字符串'月'来识别它。我需要使用:console.log(yearsData [year] ['month'] ['@ attributes']。name);
为了得到它。喔。
你应该包含一部分与问题相关的JSON – srosh 2013-03-05 19:07:50
console.log(yearsData [year] [month])的输出是什么? – Mathletics 2013-03-05 19:09:12
我编辑了上述内容以包含JSON。 console.log(yearsData [year] [month])的输出,在for(in ..)循环中是月份数据,包括属性和子级。 – Kate 2013-03-05 19:23:06