SyntaxError: JSON.parse: expected property name or

2019-04-20 09:43发布

问题:

I am trying to implement a line chart using highcharts, in which I want to color specific points.

So I am using following statement.

JSON.parse("[{x: 1,y: 0},{x:2,y:5,marker:{fillColor:'red'}},{x:3,y:8}]");

to color the point (2,5) as red.

But, it is showing error as SyntaxError: JSON.parse: expected property name or '}'

回答1:

Valid JSON strings require the property names to be quoted.

This can be corrected by quoting the property names like below:

JSON.parse('[{"x": 1, "y": 0}, {"x":2, "y":5, "marker": {"fillColor":"red"}}, {"x":3, "y":8}]');


回答2:

As it was said earlier JSON object names must to be quoted. So JSON.parse will parse only that string, valid JSON.

But if you can't for any reason change format of your string you can also parse it using eval function which can accept your syntax. But be careful! That's pretty good way for exploit.