我正在尝试解析 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);
这是抛出的错误
Uncaught SyntaxError: Unexpected token o in JSON at position 1
我需要访问 X 和 Y 值。
最佳答案
您可以直接访问x和y的值
var arr = { "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 x = arr.data.x;
var y= arr.data.y
关于javascript - JSON 解析 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267854/