проблема с localStorage и JSON.parse, ошибка: неперехваченная синтаксическая ошибка: неожиданный токен [в JSON в позиции 1430

#javascript #json #local-storage

#javascript #json #локальное хранилище

Вопрос:

У меня проблема с JSON.stringify, я объясню.

Я получаю от вызова ajax следующий JSON: введите описание изображения здесь

Всякий раз, когда я получаю похожий JSON, я хочу выполнить JSON.stringify (result.responseJSON.eventsTRS), который я добавляю в переменную, объявленную в верхней части моего JS-файла «var eventsJSON = «».

Как только JSON.stringify выполнен, это то, что я получаю: введите описание изображения здесь

затем я делаю localStorage.setItem (‘eventsJSON’, eventsJSON) и получаю: введите описание изображения здесь

Затем с моим GetItem я выполняю JSON.parse (localStorage.GetItem (‘eventsJSON’)). Out, когда я выполняю синтаксический анализ, у меня появляется следующее сообщение об ошибке: Неперехваченная синтаксическая ошибка: неожиданный токен [в JSON на позиции 1430

С чего бы это?

С уважением,

Комментарии:

1. можете ли вы показать позицию 1430 в строке?

2. На самом деле я не думал о том, чтобы посмотреть точно на содержимое моего JSON. У меня в нем 3 массива JSON, потому что в моем коде javascript я перебираю даты (и для 3 дат есть JSON). Для каждого цикла я выполняю eventsJSON = JSON.stringify (result.responseJSON.eventsTRS). eventsJSON объявляется ранее в коде (var eventsJSON = «). Что мне нужно, так это заполнять один и тот же объект JSON каждый раз, когда я его зацикливаю, а не добавлять новый объект JSON. Я не понимаю, как я могу это сделать. У вас есть идея? спасибо, с уважением,

3. Ошибка означает, что строка искажена. Если вы не покажете нам, что находится в позиции 1430, плюс части до и после, чтобы у нас был контекст, никто не сможет точно сказать, в чем проблема. Итак, если вы хотите получить ответ, вы должны показать, что вызывает проблему.

4. ХОРОШО — из этого нового кода, который действительно должен был быть обновлен в вашем вопросе, а не в качестве ответа, я вижу, что проблема в том, что строка содержит ][ дважды. Следовательно, существует три массива. Вам нужно будет заменить их на , перед использованием JSON.parse, чтобы в итоге у вас был только один массив

5. Я только что протестировал это вручную, заменив ][ на , , и данные теперь загружаются полностью как один массив.

Ответ №1:

Прошу прощения,

это мой код :

                     for(var i=start ; i<=end;i   ){
                  $.ajax({
                      dataType: "json",
                      url: "urlServlet",
                      async: false,
                      data: {
                          date: i,
                          action: 'rendementsJourTRS'
                      },
                      sucess: function(data){
                          
                      }, error : function(resultat, statut, erreur){
                          console.log(erreur);
                       }, complete : function(resultat, statut){
                           if(undefined!=resultat.responseJSON.eventsTRS amp;amp; ''!=resultat.responseJSON.eventsTRS){
                               eventsJSON  = JSON.stringify(resultat.responseJSON.eventsTRS);   

                               //Utilisation
                               localStorage.setItem('eventsJSON', eventsJSON);
                           }
                           
                          
                           $('#calendar').fullCalendar('addEventSource', resultat.responseJSON.eventsTRS);
                           
                       }
                  });
            }
  

Для каждого дня я добавляю JSON дня в eventsJSON.

Это мое содержимое localStorage.GetItem(‘eventsJSON’) :

 "[{"resourceId":"0117-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":50},{"resourceId":"0302-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-03","title":90},{"resourceId":"0308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-03","title":83},{"resourceId":"1801-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"1804-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"2701-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"2930-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":0},{"resourceId":"6601-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":68},{"resourceId":"6602-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":60},{"resourceId":"6603-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":68},{"resourceId":"6605-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":55},{"resourceId":"6609-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":34},{"resourceId":"7304-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"7308-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":2}][{"resourceId":"2117-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":72},{"resourceId":"2118-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":75},{"resourceId":"6601-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":77},{"resourceId":"6602-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":59},{"resourceId":"6603-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-04","title":60},{"resourceId":"6605-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":21},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":14},{"resourceId":"6609-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":75}][{"resourceId":"0102-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"0104-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"0124-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"0201-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":94},{"resourceId":"0203-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":90},{"resourceId":"0205-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"0206-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"0207-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":85},{"resourceId":"0218-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":99},{"resourceId":"0302-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"0308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":86},{"resourceId":"0910-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":73},{"resourceId":"0911-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":93},{"resourceId":"1212-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"1215-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1216-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"1219-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"1222-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":98},{"resourceId":"1801-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1802-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":51},{"resourceId":"1803-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1804-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2002-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2401-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":97},{"resourceId":"2402-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"2701-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2704-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"9701-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":82},{"resourceId":"1918-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":84},{"resourceId":"1919-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2117-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"2220-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":0},{"resourceId":"2221-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"2324-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2929-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":0},{"resourceId":"3201-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":82},{"resourceId":"3202-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"3401-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":69},{"resourceId":"3402-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3403-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"3601-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3604-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3701-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":69},{"resourceId":"3015-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3016-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6103-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":62},{"resourceId":"6107-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":47},{"resourceId":"6601-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":2},{"resourceId":"6602-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":61},{"resourceId":"6603-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6605-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":43},{"resourceId":"6609-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"7308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"7409-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":77},{"resourceId":"7410-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":85}]"
  

Спасибо,

Ответ №2:

Проблема в том, что строка фактически содержит три массива, а не один. В строке есть две точки (1430 является первой), где ][ вместо , замены они превращают три массива в один, который правильно загружается как полный массив. Таким образом:

 ...."title":2}][{"resourceId":"2117....
  

должно быть:

 ...."title":2},{"resourceId":"2117....
  

 let s = '[{"resourceId":"0117-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":50},{"resourceId":"0302-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-03","title":90},{"resourceId":"0308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-03","title":83},{"resourceId":"1801-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"1804-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"2701-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"2930-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":0},{"resourceId":"6601-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":68},{"resourceId":"6602-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":60},{"resourceId":"6603-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-03","title":68},{"resourceId":"6605-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":55},{"resourceId":"6609-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":34},{"resourceId":"7304-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-03","title":"  "},{"resourceId":"7308-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-03","title":2}][{"resourceId":"2117-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":72},{"resourceId":"2118-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":75},{"resourceId":"6601-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":77},{"resourceId":"6602-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":59},{"resourceId":"6603-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-04","title":60},{"resourceId":"6605-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":21},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-04","title":14},{"resourceId":"6609-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-04","title":75}][{"resourceId":"0102-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"0104-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"0124-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"0201-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":94},{"resourceId":"0203-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":90},{"resourceId":"0205-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"0206-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"0207-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":85},{"resourceId":"0218-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":99},{"resourceId":"0302-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"0308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":86},{"resourceId":"0910-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":73},{"resourceId":"0911-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":93},{"resourceId":"1212-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":87},{"resourceId":"1215-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1216-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"1219-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"1222-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":98},{"resourceId":"1801-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1802-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":51},{"resourceId":"1803-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"1804-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2002-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2401-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":97},{"resourceId":"2402-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"2701-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2704-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"9701-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":82},{"resourceId":"1918-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":84},{"resourceId":"1919-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2117-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"2220-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":0},{"resourceId":"2221-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":70},{"resourceId":"2324-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"2929-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":0},{"resourceId":"3201-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":82},{"resourceId":"3202-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"3401-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":69},{"resourceId":"3402-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3403-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":91},{"resourceId":"3601-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3604-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3701-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":69},{"resourceId":"3015-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"3016-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6103-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":62},{"resourceId":"6107-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":47},{"resourceId":"6601-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":2},{"resourceId":"6602-trs","backgroundColor":"rgb(255, 179, 0)","start":"2020-10-05","title":61},{"resourceId":"6603-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6605-trs","backgroundColor":"rgb(58, 68, 78)","start":"2020-10-05","title":"  "},{"resourceId":"6608-trs","backgroundColor":"rgb(244, 67, 54)","start":"2020-10-05","title":43},{"resourceId":"6609-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":88},{"resourceId":"7308-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":95},{"resourceId":"7409-trs","backgroundColor":"rgb(246, 220, 18)","start":"2020-10-05","title":77},{"resourceId":"7410-trs","backgroundColor":"rgb(139, 195, 74)","start":"2020-10-05","title":85}]';

let newS = s.replace(/(][)/gm, ",");

let jsonS = JSON.parse(newS);

console.log(jsonS);