Uncaught SyntaxError:JSON.parse(<anonymous>)上的JSON输入意外结束

时间:2018-06-30 20:50:55

标签: javascript json parsing stringify oracle-jet

我需要一些有关此错误的帮助:

  

未捕获的SyntaxError:JSON输入意外结束       在JSON.parse()       在Object.success(dashboard.js:22)       着火了(jquery-3.3.1.js:3268)       在Object.fireWith [as resolveWith](jquery-3.3.1.js:3398)       完成时(jquery-3.3.1.js:9305)       在XMLHttpRequest。 (jquery-3.3.1.js:9548)

我尝试使用导致该错误的 Json.parse()将字符串转换为json对象。 我正在使用oracleJet,这是我的代码:

function DashboardViewModel() {

    var self = this;
    self.lineTypeValue = ko.observable('curved');
    var scatterSeries = []; 

    $.getJSON( "http://localhost:8080/points", function (data) {
               console.info(data);
               var ch = '{"name":"graphe1","items":'+JSON.stringify(data.results[1])+ '}';
               console.info(ch);
               console.info(JSON.parse(scatterSeries));
               scatterSeries.push(JSON.parse(ch));

        });


    /* chart data */

    this.scatterSeriesValue = ko.observableArray(scatterSeries);

    self.lineTypeOptions = [
        {id: 'straight', label: 'straight'},
        {id: 'curved', label: 'curved'},
        {id: 'stepped', label: 'stepped'},
        {id: 'segmented', label: 'segmented'},
        {id: 'none', label: 'none'}
    ];


}

我从http://localhost:8080/points获得的Json看起来像这样:

{ "results":[  
  [  
     {  
        "b":"0.110547334",
        "cost":"0.000000",
        "w":"1.998889"
     }
  ],
  [  
     {  
        "x":0,
        "y":0
     },
     {  
        "x":1,
        "y":2
     },
     {  
        "x":2,
        "y":4
     },
     {  
        "x":3,
        "y":6
     },
     {  
        "x":4,
        "y":8
     },
     {  
        "x":5,
        "y":10
     },
     {  
        "x":6,
        "y":12
     },
     {  
        "x":7,
        "y":14
     },
     {  
        "x":8,
        "y":16
     },
     {  
        "x":9,
        "y":18
     },
     {  
        "x":10,
        "y":20
     },
     {  
        "x":11,
        "y":22
     },
     {  
        "x":12,
        "y":24
     },
     {  
        "x":13,
        "y":26
     },
     {  
        "x":14,
        "y":28
     },
     {  
        "x":15,
        "y":30
     },
     {  
        "x":16,
        "y":32
     },
     {  
        "x":17,
        "y":34
     },
     {  
        "x":18,
        "y":36
     },
     {  
        "x":19,
        "y":38
     },
     {  
        "x":20,
        "y":40
     },
     {  
        "x":21,
        "y":42
     },
     {  
        "x":22,
        "y":44
     },
     {  
        "x":23,
        "y":46
     },
     {  
        "x":24,
        "y":48
     },
     {  
        "x":25,
        "y":50
     },
     {  
        "x":26,
        "y":52
     },
     {  
        "x":27,
        "y":54
     },
     {  
        "x":28,
        "y":56
     },
     {  
        "x":29,
        "y":58
     },
     {  
        "x":30,
        "y":60
     },
     {  
        "x":31,
        "y":62
     },
     {  
        "x":32,
        "y":64
     },
     {  
        "x":33,
        "y":66
     },
     {  
        "x":34,
        "y":68
     },
     {  
        "x":35,
        "y":70
     },
     {  
        "x":36,
        "y":72
     },
     {  
        "x":37,
        "y":74
     },
     {  
        "x":38,
        "y":76
     },
     {  
        "x":39,
        "y":78
     },
     {  
        "x":40,
        "y":80
     },
     {  
        "x":41,
        "y":82
     },
     {  
        "x":42,
        "y":84
     },
     {  
        "x":43,
        "y":86
     },
     {  
        "x":44,
        "y":88
     },
     {  
        "x":45,
        "y":90
     },
     {  
        "x":46,
        "y":92
     },
     {  
        "x":47,
        "y":94
     },
     {  
        "x":48,
        "y":96
     },
     {  
        "x":49,
        "y":98
     },
     {  
        "x":50,
        "y":100
     },
     {  
        "x":51,
        "y":102
     },
     {  
        "x":52,
        "y":104
     },
     {  
        "x":53,
        "y":106
     },
     {  
        "x":54,
        "y":108
     },
     {  
        "x":55,
        "y":110
     },
     {  
        "x":56,
        "y":112
     },
     {  
        "x":57,
        "y":114
     },
     {  
        "x":58,
        "y":116
     },
     {  
        "x":59,
        "y":118
     },
     {  
        "x":60,
        "y":120
     },
     {  
        "x":61,
        "y":122
     },
     {  
        "x":62,
        "y":124
     },
     {  
        "x":63,
        "y":126
     },
     {  
        "x":64,
        "y":128
     },
     {  
        "x":65,
        "y":130
     },
     {  
        "x":66,
        "y":132
     },
     {  
        "x":67,
        "y":134
     },
     {  
        "x":68,
        "y":136
     },
     {  
        "x":69,
        "y":138
     },
     {  
        "x":70,
        "y":140
     },
     {  
        "x":71,
        "y":142
     },
     {  
        "x":72,
        "y":144
     },
     {  
        "x":73,
        "y":146
     },
     {  
        "x":74,
        "y":148
     },
     {  
        "x":75,
        "y":150
     },
     {  
        "x":76,
        "y":152
     },
     {  
        "x":77,
        "y":154
     },
     {  
        "x":78,
        "y":156
     },
     {  
        "x":79,
        "y":158
     },
     {  
        "x":80,
        "y":160
     },
     {  
        "x":81,
        "y":162
     },
     {  
        "x":82,
        "y":164
     },
     {  
        "x":83,
        "y":166
     },
     {  
        "x":84,
        "y":168
     },
     {  
        "x":85,
        "y":170
     },
     {  
        "x":86,
        "y":172
     },
     {  
        "x":87,
        "y":174
     },
     {  
        "x":88,
        "y":176
     },
     {  
        "x":89,
        "y":178
     },
     {  
        "x":90,
        "y":180
     },
     {  
        "x":91,
        "y":182
     },
     {  
        "x":92,
        "y":184
     },
     {  
        "x":93,
        "y":186
     },
     {  
        "x":94,
        "y":188
     },
     {  
        "x":95,
        "y":190
     },
     {  
        "x":96,
        "y":192
     },
     {  
        "x":97,
        "y":194
     },
     {  
        "x":98,
        "y":196
     },
     {  
        "x":99,
        "y":198
     }
  ]]}

我想要变量 scatterSeries 容纳的是这样的表:

[  {  
  name:"graphe1",
  items:[  
     {  
        x:8,
        y:2
     },
     {  
        x:15,
        y:15
     },
     {  
        x:25,
        y:26
     },
     {  
        x:33,
        y:22
     },
     {  
        x:36,
        y:40
     }
  ]},]

我在控制台中得到的有关字符串 ch 的信息是:

{"name":"graphe1","items":[{"x":0,"y":0},{"x":1,"y":2},{"x":2,"y":4},{"x":3,"y":6},{"x":4,"y":8},{"x":5,"y":10},{"x":6,"y":12},{"x":7,"y":14},{"x":8,"y":16},{"x":9,"y":18},{"x":10,"y":20},{"x":11,"y":22},{"x":12,"y":24},{"x":13,"y":26},{"x":14,"y":28},{"x":15,"y":30},{"x":16,"y":32},{"x":17,"y":34},{"x":18,"y":36},{"x":19,"y":38},{"x":20,"y":40},{"x":21,"y":42},{"x":22,"y":44},{"x":23,"y":46},{"x":24,"y":48},{"x":25,"y":50},{"x":26,"y":52},{"x":27,"y":54},{"x":28,"y":56},{"x":29,"y":58},{"x":30,"y":60},{"x":31,"y":62},{"x":32,"y":64},{"x":33,"y":66},{"x":34,"y":68},{"x":35,"y":70},{"x":36,"y":72},{"x":37,"y":74},{"x":38,"y":76},{"x":39,"y":78},{"x":40,"y":80},{"x":41,"y":82},{"x":42,"y":84},{"x":43,"y":86},{"x":44,"y":88},{"x":45,"y":90},{"x":46,"y":92},{"x":47,"y":94},{"x":48,"y":96},{"x":49,"y":98},{"x":50,"y":100},{"x":51,"y":102},{"x":52,"y":104},{"x":53,"y":106},{"x":54,"y":108},{"x":55,"y":110},{"x":56,"y":112},{"x":57,"y":114},{"x":58,"y":116},{"x":59,"y":118},{"x":60,"y":120},{"x":61,"y":122},{"x":62,"y":124},{"x":63,"y":126},{"x":64,"y":128},{"x":65,"y":130},{"x":66,"y":132},{"x":67,"y":134},{"x":68,"y":136},{"x":69,"y":138},{"x":70,"y":140},{"x":71,"y":142},{"x":72,"y":144},{"x":73,"y":146},{"x":74,"y":148},{"x":75,"y":150},{"x":76,"y":152},{"x":77,"y":154},{"x":78,"y":156},{"x":79,"y":158},{"x":80,"y":160},{"x":81,"y":162},{"x":82,"y":164},{"x":83,"y":166},{"x":84,"y":168},{"x":85,"y":170},{"x":86,"y":172},{"x":87,"y":174},{"x":88,"y":176},{"x":89,"y":178},{"x":90,"y":180},{"x":91,"y":182},{"x":92,"y":184},{"x":93,"y":186},{"x":94,"y":188},{"x":95,"y":190},{"x":96,"y":192},{"x":97,"y":194},{"x":98,"y":196},{"x":99,"y":198}]}

请帮忙吗?! :( :(

5 个答案:

答案 0 :(得分:3)

您正在呼叫:

JSON.parse(scatterSeries)

但是当您定义scatterSeries时,您说:

var scatterSeries = []; 

当您尝试将其解析为JSON时,它将转换为字符串(""),该字符串为空,因此在拥有JSON文本的任何可能内容之前,您要到达字符串的末尾。 / p>

scatterSeries不是JSON。不要尝试将其解析为JSON。

data也不是JSON(getJSON也会自动将其解析为JSON)。

ch是JSON…但不应如此。您应该首先创建一个普通对象:

var ch = {
    "name": "graphe1",
    "items": data.results[1]
};

scatterSeries.push(ch);

简而言之,对于您正在做的事情,您的代码中不应有JSON.parse。唯一应该在jQuery库本身中。

答案 1 :(得分:1)

问题与空数组的Json.parse-scatterSeries一起,是在按下ch之前执行scatterSeries的控制台日志

var data = { "results":[  
  [  
     {  
        "b":"0.110547334",
        "cost":"0.000000",
        "w":"1.998889"
     }
  ],
  [  
     {  
        "x":0,
        "y":0
     },
     {  
        "x":1,
        "y":2
     },
     {  
        "x":2,
        "y":4
     },
     {  
        "x":3,
        "y":6
     },
     {  
        "x":4,
        "y":8
     },
     {  
        "x":5,
        "y":10
     },
     {  
        "x":6,
        "y":12
     },
     {  
        "x":7,
        "y":14
     },
     {  
        "x":8,
        "y":16
     },
     {  
        "x":9,
        "y":18
     },
     {  
        "x":10,
        "y":20
     },
     {  
        "x":11,
        "y":22
     },
     {  
        "x":12,
        "y":24
     },
     {  
        "x":13,
        "y":26
     },
     {  
        "x":14,
        "y":28
     },
     {  
        "x":15,
        "y":30
     },
     {  
        "x":16,
        "y":32
     },
     {  
        "x":17,
        "y":34
     },
     {  
        "x":18,
        "y":36
     },
     {  
        "x":19,
        "y":38
     },
     {  
        "x":20,
        "y":40
     },
     {  
        "x":21,
        "y":42
     },
     {  
        "x":22,
        "y":44
     },
     {  
        "x":23,
        "y":46
     },
     {  
        "x":24,
        "y":48
     },
     {  
        "x":25,
        "y":50
     },
     {  
        "x":26,
        "y":52
     },
     {  
        "x":27,
        "y":54
     },
     {  
        "x":28,
        "y":56
     },
     {  
        "x":29,
        "y":58
     },
     {  
        "x":30,
        "y":60
     },
     {  
        "x":31,
        "y":62
     },
     {  
        "x":32,
        "y":64
     },
     {  
        "x":33,
        "y":66
     },
     {  
        "x":34,
        "y":68
     },
     {  
        "x":35,
        "y":70
     },
     {  
        "x":36,
        "y":72
     },
     {  
        "x":37,
        "y":74
     },
     {  
        "x":38,
        "y":76
     },
     {  
        "x":39,
        "y":78
     },
     {  
        "x":40,
        "y":80
     },
     {  
        "x":41,
        "y":82
     },
     {  
        "x":42,
        "y":84
     },
     {  
        "x":43,
        "y":86
     },
     {  
        "x":44,
        "y":88
     },
     {  
        "x":45,
        "y":90
     },
     {  
        "x":46,
        "y":92
     },
     {  
        "x":47,
        "y":94
     },
     {  
        "x":48,
        "y":96
     },
     {  
        "x":49,
        "y":98
     },
     {  
        "x":50,
        "y":100
     },
     {  
        "x":51,
        "y":102
     },
     {  
        "x":52,
        "y":104
     },
     {  
        "x":53,
        "y":106
     },
     {  
        "x":54,
        "y":108
     },
     {  
        "x":55,
        "y":110
     },
     {  
        "x":56,
        "y":112
     },
     {  
        "x":57,
        "y":114
     },
     {  
        "x":58,
        "y":116
     },
     {  
        "x":59,
        "y":118
     },
     {  
        "x":60,
        "y":120
     },
     {  
        "x":61,
        "y":122
     },
     {  
        "x":62,
        "y":124
     },
     {  
        "x":63,
        "y":126
     },
     {  
        "x":64,
        "y":128
     },
     {  
        "x":65,
        "y":130
     },
     {  
        "x":66,
        "y":132
     },
     {  
        "x":67,
        "y":134
     },
     {  
        "x":68,
        "y":136
     },
     {  
        "x":69,
        "y":138
     },
     {  
        "x":70,
        "y":140
     },
     {  
        "x":71,
        "y":142
     },
     {  
        "x":72,
        "y":144
     },
     {  
        "x":73,
        "y":146
     },
     {  
        "x":74,
        "y":148
     },
     {  
        "x":75,
        "y":150
     },
     {  
        "x":76,
        "y":152
     },
     {  
        "x":77,
        "y":154
     },
     {  
        "x":78,
        "y":156
     },
     {  
        "x":79,
        "y":158
     },
     {  
        "x":80,
        "y":160
     },
     {  
        "x":81,
        "y":162
     },
     {  
        "x":82,
        "y":164
     },
     {  
        "x":83,
        "y":166
     },
     {  
        "x":84,
        "y":168
     },
     {  
        "x":85,
        "y":170
     },
     {  
        "x":86,
        "y":172
     },
     {  
        "x":87,
        "y":174
     },
     {  
        "x":88,
        "y":176
     },
     {  
        "x":89,
        "y":178
     },
     {  
        "x":90,
        "y":180
     },
     {  
        "x":91,
        "y":182
     },
     {  
        "x":92,
        "y":184
     },
     {  
        "x":93,
        "y":186
     },
     {  
        "x":94,
        "y":188
     },
     {  
        "x":95,
        "y":190
     },
     {  
        "x":96,
        "y":192
     },
     {  
        "x":97,
        "y":194
     },
     {  
        "x":98,
        "y":196
     },
     {  
        "x":99,
        "y":198
     }
  ]]};

var scatterSeries = []; 

var ch = '{"name":"graphe1","items":'+JSON.stringify(data.results[1])+ '}';
               console.info(ch);
               
               scatterSeries.push(JSON.parse(ch));
console.info(scatterSeries);

代码示例-https://codepen.io/nagasai/pen/GGzZVB

答案 2 :(得分:1)

我试图调试同样的错误(我用 TypeScript 编写代码),但无论我做什么,错误都没有消失,它真的很持久。

我试图在许多博客和这个 SO 线程中研究解决方案。我尝试了一切,但没有任何效果。

最后,我看到我创建了名为 products.json 的所需文件,在其中我提供了一个额外的换行符(意思是,以前该文件有 1 个空行,现在有 2 个空行)并且在 {{ 3}}。

由于我的 products.json 文件中有一个额外的换行符,我浪费了 2 个小时来修复一个最初不存在的错误。

最后,当我删除整个 products.json 文件并重新运行整个程序时,我没有遇到任何运行时错误,并且一切运行正常。

这确实教会了我一件重要的事情:当操作系统或编程语言(您选择的)自动创建/删除提到的文件时,不要太聪明。一些程序员花费了大量宝贵的时间来为您自动化,所以让事情为您工作,而不是相反。

答案 3 :(得分:0)

从您的代码中删除此行:

console.info(JSON.parse(scatterSeries));

答案 4 :(得分:0)

您定义var scatterSeries = [];,然后尝试将其解析为console.info(JSON.parse(scatterSeries));处的json字符串,这显然会失败。该变量将转换为空字符串,在尝试解析该字符串时会导致“输入意外结束”错误。