SyntaxError: JSON.parse: expected property name or

2019-04-20 09:32发布

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 '}'

2条回答
萌系小妹纸
2楼-- · 2019-04-20 09:57

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.

查看更多
Anthone
3楼-- · 2019-04-20 10:03

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}]');
查看更多
登录 后发表回答