0

I am using google charts and the code to generate a chart is indicated as:

var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
    ['Mushrooms', 3],
    ['Onions', 4],
    ['Olives', 3],
    ['Zucchini', 1],
    ['Pepperoni', 2],         
]);

I am using php and I get result from a web service with ajax as string. For example I get a result as:

"[['Mushrooms', 3],['Onions', 4],['Olives', 3],['Zucchini', 1],['Pepperoni', 2],]"

I am trying to put this result in data.addRows(); for eg.

data.addRows(result);

However, it fails and generates no result. How can I append these data to the code? Thanks in advance.

JoshuaJeanThree
  • 1,382
  • 2
  • 22
  • 41

1 Answers1

1

Use JSON.parse to parse the string as JSON. Eval should be avoided if possible because it is more vulnerable to attacks.

var json = JSON.parse(result);
data.addRows(json);
Community
  • 1
  • 1
AGB
  • 2,378
  • 21
  • 37