1. ホーム
  2. javascript

[解決済み】Uncaught SyntaxError: JSON.parseで予期しないJSON入力の終了(<匿名>)

2022-02-14 05:26:26

質問

このエラーについて教えてください。

Uncaught SyntaxError: 予期しない JSON 入力の終了 JSON.parse()にて at Object.success (dashboard.js:22) 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オブジェクトに変換しようとします。 JSON.parse() というエラーが発生します。 私は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'}
    ];
  
  
}

から取得するJsonは "http://localhost:8080/points"。 はこのようになります。

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

何か手助けがあればお願いします!!! :( :(

解決方法は?

お電話でのお問い合わせです。

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ライブラリそのものだけです。