SyntaxError: JSON.parse: 使用 highcharts 时的预期属性名称或“}”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15699196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: JSON.parse: expected property name or '}' while using highcharts
提问by Rupali Shinde
I am trying to implement a line chart using highcharts, in which I want to color specific points.
我正在尝试使用 highcharts 实现折线图,我想在其中为特定点着色。
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.
将点 (2,5) 着色为红色。
But, it is showing error as SyntaxError: JSON.parse: expected property name or '}'
但是,它显示错误为 SyntaxError: JSON.parse: expected property name or '}'
回答by techfoobar
Valid JSON strings requirethe property names to be quoted.
有效的 JSON 字符串需要引用属性名称。
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}]');
回答by Roman Bekkiev
As it was said earlier JSON object names must to be quoted. So JSON.parse will parse only that string, valid JSON.
如前所述,必须引用 JSON 对象名称。因此 JSON.parse 将仅解析该字符串,即有效的 JSON。
But if you can't for any reason change format of your string you can also parse it using evalfunction which can accept your syntax. But be careful! That's pretty good way for exploit.
但是,如果您不能出于任何原因更改字符串的格式,您也可以使用eval可以接受您的语法的函数来解析它。但要小心!这是一种很好的利用方式。

