Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
Google ALWAYS have JSON API somwhere for given service. So if that is not JSON, then go look for better API its there ;) – przemo_li Mar 31 '14 at 14:50
1  
Just use " instead of ' and this will make a valid JSON. – dfsq Mar 31 '14 at 15:05
up vote 0 down vote accepted

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);

Fiddle

share|improve this answer
    
replace(/'/g, '"'); – dfsq Mar 31 '14 at 15:06
    
So, I changed the single quotes to double quotes, serverside, and then used JSON.parse(), and it all worked – Chris Ritchie Mar 31 '14 at 19:54

Use JSON.parse() on the string you get back if you can't send back a JSON object from your service to parse the string into a JSON object, but ideally, yes, you should have the response type on your service set to json.

share|improve this answer
    
I can send JSON back, but I thought that the format I have posted above, was the only format it takes. It looks like google charts does have a JSON api, which is what I need to look at. – Chris Ritchie Mar 31 '14 at 14:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.