通过javascript或JQUERY将字符串解析为JSON
parse string to JSON by javascript or JQUERY?
我的字符串如下所示。但是,通过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);
相关文章:
- 如何使用JSON字符串中的jQuery填充下拉框
- Json字符串可以'当字符串末尾有“'时,t解码;
- jquery从2个json字符串构建一个复选框表单
- RegEx只匹配JSON字符串中最里面的数组
- json字符串的Javascript帖子:没有任何东西传入或返回
- 使用javascript在MVC视图中解析.net JSON字符串
- 用Angularjs+Typescript将GZip Json字符串解压为普通Json字符串
- 如何将表单数据传递给PHP并将json字符串返回给Javascript
- 如何使用AngularJS应用基于Json字符串响应的条件CSS类
- 停止JSON.parse()从JSON字符串数据中删除尾随零
- 将JSON字符串转换为函数参数
- 在AngularJS中创建JSON字符串
- 为什么我不能使用jQuery.parseJSON(json)解析json字符串
- Json字符串更改日期
- JSON字符串格式错误
- Json和$scope的角度之间的差异$eval应用于JSON字符串时
- C#JSON字符串到JavaScript数组
- 如何在JSON字符串中实现嵌套HTML
- 如何从带有多个对象的JSON字符串创建JSON对象
- 使用捕获组查找和替换json字符串中出现的所有短语