Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

我需要一些帮助来解决这个错误:

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse () at Object.success (dashboard.js:22) at fire (jquery-3.3.1.js:3268) at Object.fireWith [as resolveWith] (jquery-3.3.1.js:3398) at done (jquery-3.3.1.js:9305) at 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 保存的是一个 table 像这样:

[  {  
  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}]}

有什么帮助吗?!! :( :(

您正在呼叫:

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 库本身。

从您的代码中删除这一行:

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

问题出在空数组的 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

您定义 var scatterSeries = [];,然后尝试将其解析为 console.info(JSON.parse(scatterSeries)); 处的 json 字符串,这显然失败了。该变量被转换为空字符串,这会在尝试解析它时导致 "unexpected end of input" 错误。

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

我试图在许多博客和这个 SO 线程中查看解决方案。我尝试了一切,但没有成功。

最后,我看到我创建了名为 products.json 的所需文件,我在其中添加了一个额外的换行符(意思是,以前该文件有 1 个空行,现在有 2 个空行)并且正在关注good GitHub practices as mentioned here.

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

最后,当我删除整个 products.json 文件并重新 运行 整个文件时,我没有遇到任何运行时错误,一切都 运行 没有任何问题。

这确实教会了我一件重要的事情:当 OS 或编程语言(您选择的)自动 creating/deleting 时,不要自作聪明文件。一些程序员花了很多时间为您自动化,所以让事情为您工作,而不是相反。

这是因为您尝试解析的数据不是可解析的格式。 所以我建议在定义解析之前检查数据是否可解析。

const IsParsable = (data) => {
            try {
                  JSON.parse(data);
                 } catch (e) {
                  return false;
                }
             return true;
    }
        
    var Obj= JSON.stringify({
                "name": "graphe1",
               "items": data.results[1]
     })

    return  IsParsable (Obj) ?  JSON.parse(Obj) : false

当我尝试从 LS

中提取 'nothing' 时,在以下函数中遇到此错误

为后代快速修复

const getUserFromLocalStorage = () => {
    try {
        return JSON.parse(localStorage.getItem('user') || '');
    } catch (error) {
        return null;
    }
};

在我的例子中,使用 .NET 6,问题是控制器被声明为 void 而没有 return 任何值。

只需将方法声明更改为 int 并添加 return 0; 就解决了这个问题。

[HttpPost]
public int MyMethod(string text)
{
    _logger.Log(text);
    return 0;
}