Having trouble generating a gantt chart from a JSON string, specifically with parsing the JSON string into a JSON object.
I have a variable myString
containing a JSON string that looks like this:
{"c": [{"v": "496"}, {"v": "Task name 1"}, {"v": "9, "}, {"v": "Date(2018,6, 19)"}, {"v": "Date(2018, 6, 21)"}, {"v": null}, {"v": 100}, {"v": null}]}
When I use
var jsonData = JSON.parse(myString);
, the values "Date(2018,6, 19)"
and "Date(2018, 6, 21)"
get changed to "Date(2018,7, 19)"
and "Date(2018, 7, 21)"
.
I am unsure what is wrong with my code. Here is the full code snippet:
$.ajax({
type: "GET",
url: URL,
data: data,
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log(response['chart_data']);
var jsonData = JSON.parse(response['chart_data']);
console.log(jsonData);
var chart_height=jsonData["rows"].length;
google.charts.load('current', {'packages': ['gantt']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(jsonData);
var options = {
width: document.getElementById("task_list").offsetWidth,
height:30*chart_height,
gantt: {
trackHeight: 30
}
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
});
SOLUTION:
Thanks to user1531038, it was noted that the new Date() function was triggering during parsing, and the Date function counts months from 0 to 11.