How to navigate "[" in JSON

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

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

I tried to do

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

to go to "startTime" (yes, the event is a variable for ajax material, everything works fine until I click "[")

Thanks for any help.

+4
source share
2 answers

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

+8
source

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

This is not a JSON question, but a JavasSript question. In javascript

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

defines an array of three elements that can be accessed using the construct t [0], t [1], t [2].

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

defines an x ​​object with the properties startTime, endTime and gd $ reminder. You cannot use "for the name of properties unless they have special characters. To access the value of the properties, use either the index conversion x [" startTime "] or the point conversion x.startTime. The x.startTime method is better and recommended.

So the answer to your question

 eventTime = event.gd$when[0].startTime 
+3
source

Source: https://habr.com/ru/post/1308832/


All Articles