2015-12-03 2 views
0

Я пытаюсь прочитать объект JSON из файла с помощью FileReader. Этот файл JSON содержит следующее:Неожиданный токен, выброшенный JSON.parse

{"markers": [ 
     { 
      "point":new GLatLng(40.266044,-74.718479), 
      "homeTeam":"Lawrence Library", 
      "awayTeam":"LUGip", 
      "markerImage":"images/red.png", 
      "information": "Linux users group meets second Wednesday of each month.", 
      "fixture":"Wednesday 7pm", 
      "capacity":"", 
      "previousScore":"" 
     }, 
     { 
      "point":new GLatLng(40.211600,-74.695702), 
      "homeTeam":"Hamilton Library", 
      "awayTeam":"LUGip HW SIG", 
      "markerImage":"images/white.png", 
      "information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.", 
      "fixture":"Tuesday 7pm", 
      "capacity":"", 
      "tv":"" 
     }, 
     { 
      "point":new GLatLng(40.294535,-74.682012), 
      "homeTeam":"Applebees", 
      "awayTeam":"After LUPip Mtg Spot", 
      "markerImage":"images/newcastle.png", 
      "information": "Some of us go there after the main LUGip meeting, drink brews, and talk.", 
      "fixture":"Wednesday whenever", 
      "capacity":"2 to 4 pints", 
      "tv":"" 
     }, 
] } 

Это код, который я написал:

var jsonClean = function(jsonText) { 
      return jsonText.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, ""); 
     }; 

     if (window.File && window.FileReader && window.FileList && window.Blob) { 
      console.log('Uploading...'); 
      var uploader = ($('input#fileUpload'))[0]; 
      var fileList = uploader.files; 
      console.log(fileList.length); 
      if(fileList.length > 0){ 
       var reader = new FileReader(); 
       reader.addEventListener('load',function(loadEvent){ 
        if(reader.readyState == FileReader.DONE){ 
         var jsonObject = jsonClean(reader.result); 
         console.log(jsonObject); 
         $('#jsonView').empty(); 
         $('#jsonView').JSONView(JSON.parse(jsonObject)); 
         } 
       }); 
        reader.readAsText(fileList[0]); 

       } 
      } 
    else { 
      alert('The File APIs are not fully supported in this browser.'); 
     } 

JSONObject, как напечатано console.log():

{"markers": [{"point":new GLatLng(40.266044,-74.718479), "homeTeam":"Lawrence Library","awayTeam":"LUGip","markerImage":"images/red.png","information": "Linux users group meets second Wednesday of each month.","fixture":"Wednesday 7pm","capacity":"","previousScore":""},{"point":new GLatLng(40.211600,-74.695702),"homeTeam":"Hamilton Library","awayTeam":"LUGip HW SIG","markerImage":"images/white.png","information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","fixture":"Tuesday 7pm","capacity":"","tv":""},{"point":new GLatLng(40.294535,-74.682012),"homeTeam":"Applebees","awayTeam":"After LUPip Mtg Spot","markerImage":"images/newcastle.png","information": "Some of us go there after the main LUGip meeting, drink brews, and talk.","fixture":"Wednesday whenever","capacity":"2 to 4 pints","tv":""},] }

«Uncaught SyntaxError : Неожиданный токен e 'вызывается при вызове JSONView().

Может кто-нибудь указать, что не так с кодом? Спасибо.

+0

Вы смешиваете объект (откуда происходит «json» - это означает «Обозначение объекта JavaScript», потому что объекты являются родными для javascript) и строку (вы не можете использовать '.replace' для объекта). Все, что имеет «новое» в нем без кавычек, недействительно JSON, но это _is_ действительный объект javascript. – h2ooooooo

ответ

0

Это недействительно JSON, для начала. Эти биты: new GLatLng(40.266044,-74.718479)

... не могут быть конструкторами класса JavaScript; измените их на один из типов, определенных в the JSON spec.

+0

.. или booleans, NULL, ints или floats, а также под-объекты. – h2ooooooo

+0

Ох .. Я просто пытался это сделать в качестве примера. Я искал файлы для json, и это то, что пришло. Спасибо что подметил это! – user2522981

+0

@ h2ooooooo ха-ха, да, я был невероятно ленив. Исправлена. – brandonscript

Смежные вопросы