开发者

How to navigate around a '[' in JSON

I'm new to JSON and moving around in it in jQuery. I'm fine until I hit a '[', as in:

  "gd$when": [{
    "startTime": "2006-11-15",
    "endTime": "2006-11-17",
    "gd$reminder": [{"minutes": "10"}]
  }],

I tried to do a

eventTime = event["gd$when"]["startTime"];

to get to the 'startTime' (Yes, event is the variable for ajax stuff, it's all working fine until I hit the '[')

Thanks for开发者_如何转开发 any help.


[ ] is an array. try event["gd$when"][0]["startTime"];


eventTime = event.gd$when[0].startTime looks more correct.

It is not a JSON question, but a JavasSript question. In JavaScript

var t = ['one','next','last'];

defines an array from three items which can be accessed by construct t[0], t[1], t[2].

var x = {
    "startTime": "2006-11-15",
    "endTime": "2006-11-17",
    "gd$reminder": [{"minutes": "10"}]
  };

defines object x with properties startTime, endTime and gd$reminder. One can don't use "" for the name of properties if they can no special characters. To access the value of properties one use either index conversion x["startTime"] or dot conversion x.startTime. The way x.startTime is better and is recommended.

So the answer on your question is

eventTime = event.gd$when[0].startTime
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜