【发布时间】:2017-01-09 02:54:23
【问题描述】:
我正在尝试解析 JSON 字符串
这是我的示例 JSON 字符串;
{
"className":"Rectangle",
"data":{
"x":685,
"y":283,
"width":179,
"height":169,
"strokeWidth":5,
"strokeColor":"#000",
"fillColor":"rgba(224,224,224,0.28)"
},
"id":"c4c49b39-d0cd-f7c7-ea89-356753051de2"
}
这是我要解析的代码;
var obj;
obj = JSON.parse({
"className": "Rectangle",
"data": {
"x": 685,
"y": 283,
"width": 179,
"height": 169,
"strokeWidth": 5,
"strokeColor": "#000",
"fillColor": "rgba(224,224,224,0.28)"
},
"id": "c4c49b39-d0cd-f7c7-ea89-356753051de2"
});
console.log(obj.className);
这是抛出的错误
未捕获的 SyntaxError:位置 1 处 JSON 中的意外标记 o
我需要访问 X 和 Y 值。
【问题讨论】:
-
您正在尝试解析对象而不是字符串.....
var obj = { "className": "Rectangle", "data": { "x": 685, "y": 283, "width": 179, "height": 169, "strokeWidth": 5, "strokeColor": "#000", "fillColor": "rgba(224,224,224,0.28)" }, "id": "c4c49b39-d0cd-f7c7-ea89-356753051de2" } ; console.log( obj.className );..或.....var obj = JSON.parse( '{ "className": "Rectangle", "data": { "x": 685, "y": 283, "width": 179, "height": 169, "strokeWidth": 5, "strokeColor": "#000", "fillColor": "rgba(224,224,224,0.28)" }, "id": "c4c49b39-d0cd-f7c7-ea89-356753051de2" }' ); console.log( obj.className );
标签: javascript json parsing