Issue
I am unsure if I am trying to achieve the impossible here, javascript is not my strong point.
This is the data structure that I need, in javascript, in order to populate a chart using google-charts:
injuryDistChart.data = [['X', '1', '2', '3', '4', '5', '6'],
[1, 2, null, null, null, null, null],
[2, 2, 3, null, null, null, null],
[3, 2, null, 4, null, 6, null],
[4, null, null, null, 5, null, null],
[5, null, null, null, null, 6, null],
[6, 2, null, null, 5, null, 7],
[7, 2, null, null, 5, null, 7],
[8, 2, null, null, 5, null, 7],
[9, 2, null, null, 5, null, 7],
[10, 2, null, null, 5, null, 7]
];
The response I am generating from the server is as follows. It is a String NOT a JSON response:
"[['X', '1', '2', '3', '4', '5', '6'],
[1, 2, null, null, null, null, null],
[2, 2, 3, null, null, null, null],
[3, 2, null, 4, null, 6, null],
[4, null, null, null, 5, null, null],
[5, null, null, null, null, 6, null],
[6, 2, null, null, 5, null, 7],
[7, 2, null, null, 5, null, 7],
[8, 2, null, null, 5, null, 7],
[9, 2, null, null, 5, null, 7],
[10, 2, null, null, 5, null, 7]
]"
Is there a way to transform this String object into the 3-D array, or am I going about this in the wrong way?
Should I rather send back a JSON response, and transform that into the 3-D array I need?
I am using Java RESTful WS, angular and google charts.
Solution
Ideally sending back JSON is the preferred method. If this isn't possible all you need to do in this instance is convert the single quotes to double quotes to make it valid JSON and then parse it like normal.
var json = str.replace("'", '"', 'g');
injuryDistChart.data = JSON.parse(json);
Answered By - Andy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.