Javascript 这个JSON字符串有什么问题?它还可以与JSONLint一起使用
这个JSON字符串似乎无效,但当我用JSONLint检查它时,它说它是有效的,所以问题出在哪里。错误在位置252处弹出Javascript 这个JSON字符串有什么问题?它还可以与JSONLint一起使用,javascript,arrays,json,object,Javascript,Arrays,Json,Object,这个JSON字符串似乎无效,但当我用JSONLint检查它时,它说它是有效的,所以问题出在哪里。错误在位置252处弹出 let test = JSON.parse('[{"id":6,"item_type":"cybro_unit","unitId":6,"pos_id":9,"name":"CyBro-2","image":"images/cybro/defaultBro.png","layer":"10","positionX":111.0,"positionY":249.0,"layerN
let test = JSON.parse('[{"id":6,"item_type":"cybro_unit","unitId":6,"pos_id":9,"name":"CyBro-2","image":"images/cybro/defaultBro.png","layer":"10","positionX":111.0,"positionY":249.0,"layerName":"10","sizeX":201.0,"sizeY":168.0,"z_index":9999,"showLabel":true,"hint":"CyBro-2\nPovezan: true\nVelikost X: 201.523163\nVelikost Y: 168.675491","enabled":true,"connected":false}]');
[{
"id": 6,
"item_type": "cybro_unit",
"unitId": 6,
"pos_id": 9,
"name": "CyBro-2",
"image": "images/cybro/defaultBro.png",
"layer": "10",
"positionX": 111.0,
"positionY": 249.0,
"layerName": "10",
"sizeX": 201.0,
"sizeY": 168.0,
"z_index": 9999,
"showLabel": true,
"hint": "CyBro-2\nPovezan: true\nVelikost X: 201.523163\nVelikost Y: 168.675491", // here at CyBro-2
"enabled": true,
"connected": false
}]
很可能是转义字符man,您是否尝试过转义\所以无论您做什么都不会将其检测为换行符 编辑: 我的意思是,我刚刚做到了,而且成功了,试试这个:
JSON.parse('[{"id":6,"item_type":"cybro_unit","unitId":6,"pos_id":9,"name":"CyBro-2","image":"images/cybro/defaultBro.png","layer":"10","positionX":111.0,"positionY":249.0,"layerName":"10","sizeX":201.0,"sizeY":168.0,"z_index":9999,"showLabel":true,"hint":"CyBro-2\\nPovezan: true\\nVelikost X: 201.523163\\nVelikost Y: 168.675491","enabled":true,"connected":false}]');
传递给
JSON.parse
的字符串中有几个文字换行符:
"hint":"CyBro-2\nPovezan: true\nVelikost X: 201.523163\nVelikost Y: 168.675491"
^^ ^^ ^^
const str='[{“id”:6,“item_type”:“cybro_unit”,“unitId”:6,“pos_id”:9,“name”:“cybro-2”,“image”:“images/cybro/defaultBro.png”,“layer”:“10”,“positionX”:111.0,“positionY”:249.0,“layerName”:“10”,“sizeX”:201.0,“sizeY”:168.0,“z_索引”:9999,“showLabel”:true,“提示”:“cybro-2\nPovezan:true\nVelikost X:201.523\nVelikost:168.6791”,“enabled”:true,“connected”:false}]';
console.log(str)代码>您没有转义换行符\n
:
let test=JSON.parse('[{“id”:6,“item_type”:“cybro_unit”,“unitId”:6,“pos_id”:9,“name”:“cybro-2”,“image”:“images/cybro/defaultBro.png”,“layer”:“10”,“positionX”:111.0,“positionY”:249.0,“layerName”:“10”,“sizeX”:201.0,“sizeY”:168.0”,“z_index”:9999,“showLabel”:true,“hint”:“cybro-2\\nPovezan:true\\nVelikost:201.523\\nveost:3163”,“enabled”:true,“connected”:false}]';
console.log(测试);
.as console wrapper{max height:100%!important;top:0;}
我将在\r\n之前尝试它,它可能会工作。我只是感到困惑,因为它在我的另一个子页面上工作。我只是添加了注释以让用户知道错误发生的位置