未捕获的 SyntaxError:JSON.parse (<anonymous>) 处的 JSON 输入意外结束
Posted
技术标签:
【中文标题】未捕获的 SyntaxError:JSON.parse (<anonymous>) 处的 JSON 输入意外结束【英文标题】:Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 【发布时间】:2018-12-09 15:10:27 【问题描述】:我需要一些帮助来解决这个错误:
未捕获的 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]
请帮忙?!! :( :(
【问题讨论】:
【参考方案1】:我试图调试同样的错误(我正在用 TypeScript 编写代码),但无论我做什么,错误都没有消失,它真的很持久。
我尝试在许多博客和这个 SO 线程中研究解决方案。我尝试了一切,但没有任何结果。
最后,我看到我创建了名为 products.json
的所需文件,其中我添加了一个额外的换行符(意思是,以前该文件有 1 个空行,现在它有 2 个空行)并在 good GitHub practices as mentioned here 之后。
由于我的 products.json
文件中有一个额外的换行符,我浪费了 2 个小时来修复一个原本不存在的错误。
最后,当我删除整个 products.json
文件并重新运行整个文件时,我没有收到任何运行时错误,并且一切运行正常。
这确实教会了我一件重要的事情:当操作系统或编程语言(您选择的)自动创建/删除提到的文件时,不要做一个聪明的裤子。一些程序员已经花费了大量时间来为你自动化事情,所以让事情为你工作,而不是反过来。
【讨论】:
【参考方案2】:您定义var scatterSeries = [];
,然后尝试将其解析为console.info(JSON.parse(scatterSeries));
的json 字符串,但显然失败了。该变量被转换为一个空字符串,这会在尝试解析它时导致“输入意外结束”错误。
【讨论】:
【参考方案3】:问题在于空数组 - scatterSeries 的 Json.parse ,因为您在推送 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
【讨论】:
【参考方案4】:从您的代码中删除这一行:
console.info(JSON.parse(scatterSeries));
【讨论】:
【参考方案5】:你在打电话:
JSON.parse(scatterSeries)
但是当你定义scatterSeries
时,你说:
var scatterSeries = [];
当您尝试将其解析为 JSON 时,它会转换为字符串 (""
),该字符串为空,因此您在获得任何可能的 JSON 文本内容之前到达字符串的末尾。
scatterSeries
不是 JSON。不要尝试将其解析为 JSON。
data
也不是 JSON(getJSON
会自动将其解析为 JSON)。
ch
是 JSON……但不应该是。您应该首先创建一个普通对象:
var ch =
"name": "graphe1",
"items": data.results[1]
;
scatterSeries.push(ch);
简而言之,对于您正在做的事情,您的代码中不应该有JSON.parse
。它应该在的唯一位置是 jQuery 库本身。
【讨论】:
以上是关于未捕获的 SyntaxError:JSON.parse (<anonymous>) 处的 JSON 输入意外结束的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的 SyntaxError:意外的令牌导出 (Redux)
未捕获的 SyntaxError:意外的令牌导入 - Reactjs
未捕获的 SyntaxError:带有 JSON.parse 的意外标记