首页 > 解决方案 > 这个 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-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
}]

标签: javascriptarraysjsonobject

解决方案


最有可能是转义字符的人,您是否尝试过转义 \ 所以无论您在做什么都不会将其检测为换行符?

编辑:

我的意思是我刚刚做到了,它奏效了,试试这个:

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}]');

推荐阅读