JSON.parse()可以从控制台运行,但不能从函数/代码

时间:2015-05-04 14:10:17

标签: javascript json

为什么将字符串解析为JSON(之前使用JSON.stringify()创建)可以从控制台运行,但是从函数中它会产生神秘错误?

console.log(output); // in console i copy it into JSON.parse() and it works
output = JSON.parse(output); // Uncaught SyntaxError: Unexpected token index:1

我的JSON:

{"type":"a","items":[{"id":"767758","id1":"1384882","id2":"1413749","c":"rgba(0, 100, 0, 5)","ls":"dashed","type":"l","d":{"t":"r","type":"1","hist":true},"w":5,"off":0},{"id":"6493942","id1":"1384882","id2":"5467332","c":"rgba(105, 105, 105, 5)","ls":"1","type":"l","d":{"t":"r","type":"h","hist":false},"w":5,"off":0},{"id":"1384882","id":"6c409d02-d937-11e4-a891-53b449010d08","d":{"t":"p","age":41,"xxx":5},"type":"n","t":"123","u":"p.png","g":[{"c":"rgb(255, 255, 255)","p":"ne","t":"5","fc":"rgb(0, 0, 0 )","w":false}],"x":-20.876105573962775,"y":41.26542299248838},{"id":"1413749","id":"e7e70a00-d3e4-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"zxc","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"42","fc":"rgb(0, 0, 0)","w":5},{"c":"rgb(0, 0, 255)","p":"nw","fc":"rgb(0, 0, 0)"}],"x":149.06285284387724,"y":5.308329729351229},{"id":"5467332","id":"8f0f5c30-d3d9-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"asd","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"","fc":"rgb(0, 0, 0)","w":false},{"p":"nw","fc":"rgb(0, 0, 0)"}],"x":-164.24347467678655,"y":-32.64876353378594}],"combos":{"iop":[],"dfg":[]},"jk":{"width":966,"height":890,"zoom":5,"offsetX":905,"offsetY":744}}

我的代码,有些东西缺失,因为其他人给了JSfiddles工作:

var memory = '';

$buttonSave.click(function (event) {
    if (helpers.isNUE(chart)) { return; }

    var data = chart.serialize();

    data = JSON.stringify(data).split(''); // string to array

    data.forEach(function (datum) { // foreach character
        memory += datum.charCodeAt(0).toString(2) + '2'; // get binary charcode, add padding "2"
    });

    console.info('memory saved: ' + memory);

    event.preventDefault();
    return false;
});

$buttonLoad.click(function (event) {
    var data = memory.split('2'), // get binary code for each character
        output = '',
        serializedChart = {};

    data.forEach(function (datum) {
        output += String.fromCharCode(parseInt(datum, 2)); // read binary charcode and get character from it
    });

    console.warn('load done:');

    try {
        serializedChart = JSON.parse(output);
    } catch (e) {
        console.warn(e);
    }
});

4 个答案:

答案 0 :(得分:1)

您缺少json字符串中的最终}。用这个:

{
  "type": "a",
  "items": [
    {
      "id": "767758",
      "id1": "1384882",
      "id2": "1413749",
      "c": "rgba(0, 100, 0, 5)",
      "ls": "dashed",
      "type": "l",
      "d": {
        "t": "r",
        "type": "1",
        "hist": true
      },
      "w": 5,
      "off": 0
    },
    {
      "id": "6493942",
      "id1": "1384882",
      "id2": "5467332",
      "c": "rgba(105, 105, 105, 5)",
      "ls": "1",
      "type": "l",
      "d": {
        "t": "r",
        "type": "h",
        "hist": false
      },
      "w": 5,
      "off": 0
    },
    {
      "id": "6c409d02-d937-11e4-a891-53b449010d08",
      "d": {
        "t": "p",
        "age": 41,
        "xxx": 5
      },
      "type": "n",
      "t": "123",
      "u": "p.png",
      "g": [
        {
          "c": "rgb(255, 255, 255)",
          "p": "ne",
          "t": "5",
          "fc": "rgb(0, 0, 0 )",
          "w": false
        }
      ],
      "x": -20.876105573962775,
      "y": 41.26542299248838
    },
    {
      "id": "e7e70a00-d3e4-11e4-b3ef-53b449010d08",
      "d": {
        "t": "c",
        "active": true,
        "r": 47
      },
      "type": "n",
      "t": "zxc",
      "u": "p.png",
      "g": [
        {
          "c": "#ccff99",
          "p": "ne",
          "t": "42",
          "fc": "rgb(0, 0, 0)",
          "w": 5
        },
        {
          "c": "rgb(0, 0, 255)",
          "p": "nw",
          "fc": "rgb(0, 0, 0)"
        }
      ],
      "x": 149.06285284387724,
      "y": 5.308329729351229
    },
    {
      "id": "8f0f5c30-d3d9-11e4-b3ef-53b449010d08",
      "d": {
        "t": "c",
        "active": true,
        "r": 47
      },
      "type": "n",
      "t": "asd",
      "u": "p.png",
      "g": [
        {
          "c": "#ccff99",
          "p": "ne",
          "t": "",
          "fc": "rgb(0, 0, 0)",
          "w": false
        },
        {
          "p": "nw",
          "fc": "rgb(0, 0, 0)"
        }
      ],
      "x": -164.24347467678655,
      "y": -32.64876353378594
    }
  ],
  "combos": {
    "iop": [],
    "dfg": []
  },
  "jk": {
    "width": 966,
    "height": 890,
    "zoom": 5,
    "offsetX": 905,
    "offsetY": 744
  }
}

答案 1 :(得分:0)

输出是否已解析?如果是这样,看起来你正在尝试解析输出两次,如果输出已经是JSON值,则不需要再次解析它。

答案 2 :(得分:0)

您在JSON结尾处错过}。如果你添加它应该工作。

{"type":"a","items":[{"id":"767758","id1":"1384882","id2":"1413749","c":"rgba(0, 100, 0, 5)","ls":"dashed","type":"l","d":{"t":"r","type":"1","hist":true},"w":5,"off":0},{"id":"6493942","id1":"1384882","id2":"5467332","c":"rgba(105, 105, 105, 5)","ls":"1","type":"l","d":{"t":"r","type":"h","hist":false},"w":5,"off":0},{"id":"1384882","id":"6c409d02-d937-11e4-a891-53b449010d08","d":{"t":"p","age":41,"xxx":5},"type":"n","t":"123","u":"p.png","g":[{"c":"rgb(255, 255, 255)","p":"ne","t":"5","fc":"rgb(0, 0, 0 )","w":false}],"x":-20.876105573962775,"y":41.26542299248838},{"id":"1413749","id":"e7e70a00-d3e4-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"zxc","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"42","fc":"rgb(0, 0, 0)","w":5},{"c":"rgb(0, 0, 255)","p":"nw","fc":"rgb(0, 0, 0)"}],"x":149.06285284387724,"y":5.308329729351229},{"id":"5467332","id":"8f0f5c30-d3d9-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"asd","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"","fc":"rgb(0, 0, 0)","w":false},{"p":"nw","fc":"rgb(0, 0, 0)"}],"x":-164.24347467678655,"y":-32.64876353378594}],"combos":{"iop":[],"dfg":[]},"jk":{"width":966,"height":890,"zoom":5,"offsetX":905,"offsetY":744}}

答案 3 :(得分:0)

什么是typeof output?要使parse()生效,它必须为string,但问题中的转储似乎会返回objectobject表示已经解析了JSON,它是一个JavaScript对象。

所以这会奏效:

var output = '{"type":"a","items":[{...}]}';
output = JSON.parse(output);

虽然这不会:

var output = {
    "type":"a",
    "items":[{...}]
};
output = JSON.parse(output);
相关问题