我有一个循环的对象。在我循环对象之前,我决定将值存储在单独的变量中,为此我使用对象表示法来获取值..我对“选项”和“行”未定义 - 这两者都有错访问?
或任何人建议我访问这些节点的正确方法。
这是我的目标:
var ob = {
"DashBoard":[
{"title":"DashBoard"},
{"widget":[{"slide":"To do"},{"slide":"Teamspace"},{"slide":"Recent Activity"}]},
{"activity":[
{"option":[{"show":"Volvo"},{"show":"Benz"},{"show":"Honda"}]},
{"rows":[
{"title":"Advert1", "text":"sample text1", "date":"22-06-2013"},
{"title":"Advert2", "text":"sample text2", "date":"22-06-2014"},
{"title":"Advert3", "text":"sample text3", "date":"22-06-2015"},
{"title":"Advert4", "text":"sample text4", "date":"22-06-2016"},
{"title":"Advert5", "text":"sample text5", "date":"22-06-2017"}
]}
]}
]
}
var title = ob.DashBoard[0], widget= ob.DashBoard[1], option = ob.DashBoard[2]["option"], rows = ob.DashBoard[2]["rows"];
console.log(option); //undefined
console.log(rows); //undefined
我在两种情况下得到的结果都是未定义的......
答案 0 :(得分:3)
阅读ob.DashBoard[2].activity[0]["option"] and ob.DashBoard[2].activity[1]["option"].
你将能够阅读你的json