Reputation: 993
I have a javascript variable which looks like:
data = [{
y: 55.11,
color: colors[0],
drilldown: {
name: 'MSIE versions',
categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
data: [10.85, 7.35, 33.06, 2.81],
color: colors[0]
}
}, {
y: 21.63,
color: colors[1],
drilldown: {
name: 'Firefox versions',
categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
data: [0.20, 0.83, 1.58, 13.12, 5.43],
color: colors[1]
}
}, {
y: 11.94,
color: colors[2],
drilldown: {
name: 'Chrome versions',
categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0',
'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'],
data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22],
color: colors[2]
}
}, {
y: 7.15,
color: colors[3],
drilldown: {
name: 'Safari versions',
categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon',
'Safari 3.1', 'Safari 4.1'],
data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14],
color: colors[3]
}
}, {
y: 2.14,
color: colors[4],
drilldown: {
name: 'Opera versions',
categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'],
data: [ 0.12, 0.37, 1.65],
color: colors[4]
}
}];
I want this variable to create from servlet according to data in my database when I'm passing this value from servlet as normal response type (response.setContentType("text/html;charset=UTF-8");)
I'm unable to process it. Even if you see this variable its not a valid json object so I can't use JSON.parse.
Please guide me how to get this variable through ajax call what will be dataType in ajax call and what will be response content type in servlet. Or if there is some other way to do it please help. Thanks
Upvotes: 2
Views: 950
Reputation: 8694
Since your data references other objects (colors
), thus making it JSON incompatible, why not use eval
?
var colors = [1,2,3,4,5,6,7,8];
var data;
eval("data = [{...}]");
...
Upvotes: 1
Reputation: 152
At the server side you can create bean class( or called as DTO objects) with the properties and set the values to it and while sending it back change to the return type as JSON.It will parse to json string and send it back to client side there you can use parseJSON to change from json string to json object.
Upvotes: 0
Reputation: 10595
On the servlet side, write the text to a text/plain response:
response.getOutputStream().write(myKindaJsonString.getBytes());
Use jquery's ajax to get it:
$.ajax({
url: CONTEXT_PATH + '/YourServlet',
processData: false,
type: "GET",
success: function(response) {
// your string is response now
},
error: function(response) {
alert("There was an error while trying to get value.");
}
});
Upvotes: 3