这个JSON字符串有什么问题?它还与Jsonlint一起使用



此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,"layerName":"10","sizeX":201.0,"sizeY":168.0,"z_index":9999,"showLabel":true,"hint":"CyBro-2nPovezan: truenVelikost X: 201.523163nVelikost 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-2nPovezan: truenVelikost X: 201.523163nVelikost Y: 168.675491", // here at CyBro-2
    "enabled": true,
    "connected": false
}]

很可能是逃脱角色人,您是否尝试过逃脱 ,所以您所做的任何事情都不会将其视为断路?

编辑:

我的意思是我只是做到了,它奏效了,请尝试以下操作:

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的字符串中有几个字面的newline 字符:

"hint":"CyBro-2nPovezan: truenVelikost X: 201.523163nVelikost 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_index":9999,"showLabel":true,"hint":"CyBro-2nPovezan: truenVelikost X: 201.523163nVelikost Y: 168.675491","enabled":true,"connected":false}]';
console.log(str);

newline字符在JSON中无效 - 相反,您应该具有字面的,然后是字面的n,以表明对象中的分析字符串应包含字面的newline。您可以通过仔细检查 s来表示单个字面的,例如:

"hint":"CyBro-2\nPovezan: true\nVelikost X: 201.523163\nVelikost Y: 168.675491"

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}]'
);
console.log(test);

您尚未逃脱您的newline字符 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 X: 201.523163\nVelikost Y: 168.675491","enabled":true,"connected":false}]');
console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您也可以通过用\n替换所有n来做到这一点:

let test = '[{"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}]';
test = test.replace(/n/g, "\n");
console.log(JSON.parse(test));
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新