【问题标题】:What is wrong with this JSON string? Also it work with JSONLint这个 JSON 字符串有什么问题?它也适用于 JSONLint
【发布时间】:2019-03-21 08:12:39
【问题描述】:

这个 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
}]

【问题讨论】:

  • 我只是添加了评论让用户知道错误发生在哪里

标签: javascript arrays json object


【解决方案1】:

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

编辑:

我的意思是我刚刚做了它并且它有效,试试这个:

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

【讨论】:

  • 我会在 \n 之前用 \r 尝试它,它可能会起作用我只是感到困惑,因为它适用于我的另一个子页面。
【解决方案2】:

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

换行符在 JSON 中无效 - 相反,您应该有一个文字 \ 后跟一个文字 n 以指示对象中的已解析字符串应包含文字换行符。您可以通过双重转义 \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);

【讨论】:

    【解决方案3】:

    你没有逃脱你的换行符\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; }

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 1970-01-01
      • 2010-12-31
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多