我的字符串如下所示。但是当通过javascript / jQuery将其转换为JSON时,会收到错误" Invalid Charater"。请帮忙
'[{ color: "#8B0000", data: [{ "y": 12000, "x": 0}], pointRange: 8, type: "column", name: "Segment1"}, { color: "#FFA500", data: [{ "y": 11000, "x": 10}], pointRange: 12, type: "column", name: "Segment2"}, { color: "#11ddbb", data: [{ "y": 10000, "x": 19}], pointRange: 6, type: "column", name: "Segment3"}, { color: "#8B0000", data: [{ "y": 8000, "x": 24}], pointRange: 5, type: "column", name: "Segment4"}]'
这是我用来将其解析为JSON的代码。
var response ='[{ color: "#8B0000", data: [{ "y": 12000, "x": 0}], pointRange: 8, type: "column", name: "Segment1"}, { color: "#FFA500", data: [{ "y": 11000, "x": 10}], pointRange: 12, type: "column", name: "Segment2"}, { color: "#11ddbb", data: [{ "y": 10000, "x": 19}], pointRange: 6, type: "column", name: "Segment3"}, { color: "#8B0000", data: [{ "y": 8000, "x": 24}], pointRange: 5, type: "column", name: "Segment4"}]';
response=$.parseJSON(response);
alert(response);
答案 0 :(得分:0)
原始的响应消息是无效的json。
选项1:
你拥有的是一个Javascript对象(如果你删除那些单引号)。你没有JSON字符串而是Javascript对象。
从开头删除引号后,它看起来像
var response = [{ color: "#8B0000", data: [{ "y": 12000, "x": 0}], pointRange: 8, type: "column", name: "Segment1"}, { color: "#FFA500", data: [{ "y": 11000, "x": 10}], pointRange: 12, type: "column", name: "Segment2"}, { color: "#11ddbb", data: [{ "y": 10000, "x": 19}], pointRange: 6, type: "column", name: "Segment3"}, { color: "#8B0000", data: [{ "y": 8000, "x": 24}], pointRange: 5, type: "column", name: "Segment4"}];
var jsonString = JSON.stringify(response);
您现在可以选择使用JSON.stringify()
将其转换为JSON字符串。
您还有一个选项2:
为所有键值添加双引号,这也是有效的。这将使它成为一个JSON字符串。然后,您可以使用JSON.parse()
将其转换为JSON对象。
var response = '[{ "color": "#8B0000", "data": [{ "y":12000, "x":0}], "pointRange": 8, "type": "column", "name": "Segment1"}, { "color": "#FFA500", "data": [{ "y": 11000, "x": 10}], "pointRange": 12, "type": "column", "name": "Segment2"}, { "color": "#11ddbb", "data": [{ "y": 10000, "x": 19}], "pointRange": 6, "type": "column", "name": "Segment3"}, { "color": "#8B0000", "data": [{ "y": 8000, "x": 24}], "pointRange": 5, "type": "column", "name": "Segment4"}]';
var jsonObject = JSON.parse(response);
答案 1 :(得分:0)
如果您有时间,请访问http://www.json.org/。在那里你会发现JSON的规范。
你的一个问题是:对象中的键必须是双引号中的字符串。
例如:
'{ color: "#8B0000" }'
错了
必须是:
'{ "color": "#8B0000" }'
答案 2 :(得分:0)
以下可能是解决方案
var response ='[{ color: "#8B0000", data: [{ "y": 12000, "x": 0}], pointRange: 8, type: "column", name: "Segment1"}, { color: "#FFA500", data: [{ "y": 11000, "x": 10}], pointRange: 12, type: "column", name: "Segment2"}, { color: "#11ddbb", data: [{ "y": 10000, "x": 19}], pointRange: 6, type: "column", name: "Segment3"}, { color: "#8B0000", data: [{ "y": 8000, "x": 24}], pointRange: 5, type: "column", name: "Segment4"}]';
var Obj=eval(response);
var response=JSON.stringify(Obj);
alert(response);
答案 3 :(得分:0)
在Javascript json字符串中将所有键转换为带引号的字符串,如颜色到“颜色”等。 < / p>
var response = '[{ "color": "#8B0000", "data": { "y": 12000, "x": 0}, "pointRange": 8, "type": "column", "name": "Segment1"}, { "color": "#FFA500", "data": [{ "y": 11000, "x": 10}], "pointRange": 12, "type": "column", "name": "Segment2"}, { "color": "#11ddbb", "data": [{ "y": 10000, "x": 19}], "pointRange": 6, "type": "column", "name": "Segment3"}, { "color": "#8B0000", "data": [{ "y": 8000, "x": 24}], "pointRange": 5, "type": "column", "name": "Segment4"}]';
response=JSON.parse(response);
console.log(response);
答案 4 :(得分:0)
@Monsta - 你在密钥中缺少双引号;代码应该是这样的:
var response ='[{ "color": "#8B0000", "data": [{ "y":12000, "x":0}], "pointRange": 8, "type": "column", "name": "Segment1"}, { "color": "#FFA500", "data": [{ "y": 11000, "x": 10}], "pointRange": 12, "type": "column", "name": "Segment2"}, { "color": "#11ddbb", "data": [{ "y": 10000, "x": 19}], "pointRange": 6, "type": "column", "name": "Segment3"}, { "color": "#8B0000", "data": [{ "y": 8000, "x": 24}], "pointRange": 5, "type": "column", "name": "Segment4"}]';
JSON.parse(response);