0

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.

2
  • Google ALWAYS have JSON API somwhere for given service. So if that is not JSON, then go look for better API its there ;) Commented Mar 31, 2014 at 14:50
  • 1
    Just use " instead of ' and this will make a valid JSON. Commented Mar 31, 2014 at 15:05

3 Answers 3

1

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.

const json = str.replaceAll("'", '"');
injuryDistChart.data = JSON.parse(json);

Example:

const json = "[['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]]";
const json2 = json.replaceAll("'", '"');
console.log(JSON.parse(json2))

Sign up to request clarification or add additional context in comments.

2 Comments

replace(/'/g, '"');
So, I changed the single quotes to double quotes, serverside, and then used JSON.parse(), and it all worked
0

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.

1 Comment

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.
0

const json = "[['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]]"
const json2 = eval(json)
console.log(json2)

3 Comments

Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
eval is unsafe. It can easily be exploited by providing a response with malicious code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.