I have created a JSON like this one :
{"data":[{"content":"Event1","start":"new Date(2014,07,10)"},{"content":"Event2","start":"new Date(2014,07,17)"}],"success":true}
In order to fit the following code in Javascript :
data = [
{
'start': new Date(2010,7,23),
'content': 'Event'
},
{
'start': new Date(2010,7,23),
'content': 'Event'
},
];
Witth th JSON i have, i can easily access field, for example :
json.data[0].content
Return : "Event1"
My question is : how can i make the Javascript code "dynamic" to load every components in my JSON, assuming the fact that i don't know how many elements it will contains ?
Currently, i've done the followig code :
var xhr = new XMLHttpRequest();
xhr.open("GET", "/Test", true); // Call to a java servlet
xhr.send();
var json;
var jsonLength;
var data;
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
json = JSON.parse(xhr.responseText);
jsonLength = json.data.length;
}
}
data = [];
for(var i = 0 ; i < jsonLength ; i++){
data.push({
'start':json.data[i]['start'],
'content':json.data[i]['content']
})
}
timeline.draw(data);
EDIT : I don't even enter to the for loop ... Why ?