通过javascript或JQUERY将字符串解析为JSON

parse string to JSON by javascript or JQUERY?

本文关键字:JSON 字符串 javascript JQUERY 通过      更新时间:2023-09-26

我的字符串如下所示。但是,通过javascript/jQuery将其转换为JSON时,收到的错误是"无效字符"。请帮助

'[{ 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);

如果您有时间,请访问http://www.json.org/。在这里,您将找到JSON规范。

你的一个问题是:对象中的键必须是带双引号的字符串。

例如

:'{ color: "#8B0000" }'是错误的

必须是:'{ "color": "#8B0000" }'

下面可能是一个解决方案

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);

Javascript中的json字符串将所有键转换为引号字符串,如 color"color"

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);

原始响应消息是无效的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);

@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);