简体   繁体   English

localStorage 和 JSON.parse 的问题,错误:Uncaught SyntaxError: Unexpected token [ in JSON at position 1430

[英]problem with localStorage and JSON.parse, error : Uncaught SyntaxError: Unexpected token [ in JSON at position 1430

I have a problem with JSON.stringify, I'll explain.我对 JSON.stringify 有问题,我会解释。

I receive from an ajax call the following JSON:我从 ajax 调用收到以下 JSON: 在此处输入图片说明

Whenever I receive a similar JSON, I want to do a JSON.stringify (result.responseJSON.eventsTRS) that I add in a variable declared at the top of my JS file "var eventsJSON = ''".每当我收到类似的 JSON 时,我都想做一个 JSON.stringify (result.responseJSON.eventsTRS),我将它添加到我的 JS 文件“var eventsJSON = ''”顶部声明的变量中。

Once the JSON.stringify is done, this is what I get:一旦 JSON.stringify 完成,这就是我得到的: 在此处输入图片说明

then I do a localStorage.setItem ('eventsJSON', eventsJSON) and I get:然后我做一个 localStorage.setItem ('eventsJSON', eventsJSON) 我得到: 在此处输入图片说明

Then with my getItem I do a JSON.parse (localStorage.getItem ('eventsJSON')).然后用我的 getItem 我做一个 JSON.parse (localStorage.getItem ('eventsJSON'))。 Out, when I do my parse, I have the following error message: Uncaught SyntaxError: Unexpected token [in JSON at position 1430当我进行解析时,我收到以下错误消息:Uncaught SyntaxError: Unexpected token [in JSON at position 1430

Why would you be?你为什么会是?

Regards,问候,

I am sorry,对不起,

this is my code :这是我的代码:

                    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 && ''!=resultat.responseJSON.eventsTRS){
                               eventsJSON += JSON.stringify(resultat.responseJSON.eventsTRS);   

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

For each day, I add a JSON of the day in eventsJSON.对于每一天,我都会在 eventsJSON 中添加当天的 JSON。

This is my localStorage.getItem('eventsJSON') contents :这是我的 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}]"

Thank you,谢谢,

The issue is that the string actually contains three array, not one.问题是字符串实际上包含三个数组,而不是一个。 There are two points in the string (1430 being the first), where there is ][ instead of , Replacing these turns the three arrays into one, which loads as a full array correctly.字符串中有两个点(1430 是第一个),其中有][而不是,替换这些会将三个数组变成一个,正确加载为完整数组。 Thus:因此:

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

should be:应该:

...."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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) JSON.parse 错误(未捕获的 SyntaxError:JSON 中的意外标记 o 在 position 1) - JSON.parse error (Uncaught SyntaxError: Unexpected token o in JSON at position 1) 未捕获到的SyntaxError:意外令牌[与JSON.parse - Uncaught SyntaxError: Unexpected token [ with JSON.parse "未捕获的 SyntaxError:带有 JSON.parse 的意外标记" - Uncaught SyntaxError: Unexpected token with JSON.parse JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM