I have the following JS code:
var response = loadXMLDoc();
var dataset = response.data;
alert(response);
alert (dataset);
"alert(response)" prints this:
{"labels":["-inf - 10","10 - 20","20 - 30","30 - 40","40 - 50","50 - 60","60 - 70","70 - 80","80 - 90","90 - 100","100 - 110","110 - 120","120 - 130","130 - 140","140 - 150","150 - 160","160 - +inf"],"data":[3,8,7,3,7,6,6,7,5,4,10,7,4,4,7,2,0],"count":16}
while "alert(dataset)" gives "undefined". I have tried to use
var dataset = response["data"];
but it did not work as well. I want to get the data array from the JSON object. How can i do that. Thanks
Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked May 28, 2012 at 10:50
Sami
7,84719 gold badges47 silver badges71 bronze badges
3 Answers 3
Use var y = JSON.parse(response); alert(y["data"])
answered May 28, 2012 at 10:57
rt2800
3,0552 gold badges23 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Seeing that you got alert to show response, it's a string, not yet an object.
You need to parse it with JSON.parse()
//load your response
var response = loadXMLDoc(),
dataset;
//parse response
response = JSON.parse(response);
//assign data to dataset
dataset = response.data;
//Hit F12 to see the console
console.log(response);
console.log(dataset);
answered May 28, 2012 at 10:53
Joseph
120k31 gold badges186 silver badges238 bronze badges
2 Comments
Jill-Jênn Vie
Your jsFiddle ID is quite insulting to Europeans.
Joseph
@Jill-JênnVie lol, tell that to JSFiddle. It's not my fault XD
Try this
var dataset = eval('(' + responce.data + ')');
answered May 28, 2012 at 11:08
Talha
19.3k8 gold badges53 silver badges66 bronze badges
1 Comment
Daniele B
responce.d maybe you meant response.data?lang-js
datain the JSON (onlylabelsandcount).