访问嵌套属性

时间:2012-02-17 05:32:25

标签: javascript json

我有这样的代码......

function diagnoseprint() {
document.getElementById('goeshere').innerHTML=localStorage['timetable'];
var decode = JSON.parse(localStorage['timetable']);
var step = decode.timetable.UCFF1201ENG.2;
document.getElementById('goeshere').innerHTML=step[1];}

和localStorage ['timetable']实际上是500KB的东西我不打算在这里发布所有这些,我发布了一点...

{"meta":{"week":"2012-02-13","lastsync":"1329415763","jsontime":1329456671},"timetable":{"TPMF1101MBA":{"1":["MON,13-Feb-12","08:45 - 10:15","L2 - 12","TPM","BSB20133-M-BRM-L","DR. TAN JUAT HONG"],"2":["WED,15-Feb-12","10:35 - 12:05","L2 - 12","TPM","BSB20133-M-BRM-T","DR. TAN JUAT HONG"]},

我想知道为什么我无法提取这一点,顺便说一下,我刚开始使用JavaScript。

1 个答案:

答案 0 :(得分:2)

而不是:

var step = decode.timetable.UCFF1201ENG.2;

使用:

var step = decode.timetable.UCFF1201ENG[2];

2不是有效的标识符,因此不能在点表示法中使用。使用括号表示法,它完全相同。