首页 > 解决方案 > 从代码“JSON 输入意外结束”中获取以下错误消息

问题描述

试图将一些内容保存到 JSON 对象中,但是它似乎在某些情况下会中断,例如任何单词都有撇号:我的函数如下所示:

function escapeHtml(text) {
        var map = {
          '&': '',
          '<': '',
          '>': '',
          '"': '',
          "'": '',
          '[': '',
          ']': ''
        };
        return text.replace(/[&<>"']/g, function(m) { return map[m]; });
      }

代码的另一个功能:

  function updateVariable() {
    $('input.selected_programs').val('-77');
    if ($('#selected_programs li').length) {
      $('#selected_programs').show();
        $('#selected_programs li').each(function(){
          var program = JSON.parse($(this).attr('program-data')).split(',');
          var program_details;
          programId = escapeHtml(program[0]);
          for (var h = 1; h < program.length; h++) {
              program[h] = escapeHtml(program[h]);
              program[h] = program[h].replace(/\\'/g,"&#039;");
              program[h] = program[h].replace(/\\&#039;/g,"&#039;");
              program[h] = program[h].replace(/&amp;/g,"&");
              program[h] = program[h].replace(/\]/g,"");
            if (trim(program[h]) != "" && program[h] != 0 && program[h] != "-") {
              if (h == 1) {
                program_details = "<strong>"+program[h] + "</strong><br />";
              } else if (h == 2) {
                program_details = program_details + " Series: " + program[h] +";";
              } else if (h == 3) {
                program_details = program_details + " Episode: " + program[h] +";";
              } else if (h == 4) {
                program_details = program_details + " " + program[h] +";";
              } else if (h == 5) {
                program_details = program_details + " " + program[h];
              }

            }
          }
          $('input.selected_programs').each(function(){
            if($(this).val() == -77){
              $(this).val(program_details);
              return false;
            }
          });
        });
    } else {
      $('#selected_programs').hide();
    }

这似乎在大多数情况下都适用于从文件返回的格式。但是,我注意到在某些时候,例如“Nicklaus's”这个词中有一个“'”,那么我的代码不会忽略撇号并破坏 JSON 格式。我不确定我在哪里失败了

下面是代码中断的示例和另一个可以正常工作的示例:

这个工作正常:

<li class="ui-li-static ui-body-inherit" program-id="1670271" program-data="&quot;[\&quot;1670271\&quot;,\&quot;2000: Official Top 10 (MTV Music (IRE))\&quot;,\&quot;0\&quot;,\&quot;0\&quot;,\&quot;\&quot;,\&quot;Tune in to enjoy the biggest hits of 2000 (Music)\&quot;,\&quot;40037\&quot;,\&quot;2\&quot;]&quot;"><img src="../../../uc/custom/tpl_layout_responsive3/questiontypes/special/cQ2v2/resources/read.png" class="remove_program">2000: Official Top 10 (MTV Music (IRE)), Tune in to enjoy the biggest hits of 2000 (Music)</li>

以下代码似乎打破了撇号指向的地方:

<li class="ui-li-static ui-body-inherit" program-id="1643452" program-data="&quot;[\&quot;1643452\&quot;,\&quot;1975: The Magnificent Masters (Sky Sports Golf)\&quot;,\&quot;0\&quot;,\&quot;0\&quot;,\&quot;\&quot;,\&quot;A look back at Jack Nicklaus\\\\" s="" triumph="" in="" the="" 1975="" masters="" tournament="" at="" augusta="" national="" golf="" club.="" golden="" bear="" held="" his="" nerve="" to="" win="" fifth="" green="" jacket="" (sports)\",\"51609\",\"2\"]"'=""><img src="../../../uc/custom/tpl_layout_responsive3/questiontypes/special/cQ2v2/resources/read.png" class="remove_program">1975: The Magnificent Masters (Sky Sports Golf), A look back at Jack Nicklaus's triumph in the 1975 Masters tournament at Augusta National Golf Club. The Golden Bear held his nerve to win his fifth green jacket (Sports)</li>

对此的任何帮助将不胜感激。

如果它有帮助,这就是我使用 programData 的地方

if (programData.displayCount > 0) {
                for (var i = 0; i < programData.data.length; i++) {
                  var program = JSON.parse(programData.data[i]);
                  var program_details;
                  var program_description;
                  for (var h = 1; h < program.length; h++) {
                    if (program[h] != "" && program[h] != 0 && program[h] != "-") {
                      program[h] = program[h].replace(/\\'/g,"&#039;");
                      program[h] = program[h].replace(/\\&#039;/g,"&#039;");
                      program[h] = program[h].replace(/&amp;/g,"&");
                      if (program_details && h < 6) {
                        program_details = program_details + ", ";
                        program_description = program_description + ", ";
                      }
                      if (h == 2) {
                        program_details = program_details + "Series: ";
                        program_description = program_description + "Series: ";
                      } else if (h == 3) {
                        program_details = program_details + "Episode: ";
                        program_description = program_description + "Episode: ";
                      } else if (h == 4) {
                        program_details = program_details;
                        program_description = program_description;
                      } else if (h == 5) {
                        program_details = program_details;
                        program_description = program_description;
                      }
                      if (h < 6) {
                        if (h != 1) {
                          program_details = program_details + program[h];
                          program_description = program_description + program[h];
                        } else {
                          program_details = program[h];
                          program_description = program[h];
                        }
                      }
                      if (h==4 || h==5) {
                        program_description = program_description;
                      }
                    }
                  }
                  $("#program_select").append("<option class='select-option' value='"+program[0]+"' program-details ='"+program_details+"' program-data='"+JSON.stringify(programData.data[i])+"' title='"+program[5]+"'>"+program_description+"</option>");
                }

根据大多数要求添加额外的 HTML:

这是在添加到列表之前损坏的部分的 HTML:

<option class="select-option" value="1413499" program-details="13 Minutes: The Plot to Assassinate Adolf Hitler (BBC Two Northern Ireland), World War II drama. In November 1939, Georg Elser's attempt to assassinate Hitler fails, and he is arrested. During his confinement, he recalls the events leading up to his plot. (Movie )" program-data="&quot;[\&quot;1413499\&quot;,\&quot;13 Minutes: The Plot to Assassinate Adolf Hitler (BBC Two Northern Ireland)\&quot;,\&quot;0\&quot;,\&quot;0\&quot;,\&quot;\&quot;,\&quot;World War II drama. In November 1939, Georg Elser\\\\'s attempt to assassinate Hitler fails, and he is arrested. During his confinement, he recalls the events leading up to his plot. (Movie )\&quot;,\&quot;519\&quot;,\&quot;2\&quot;]&quot;" title="World War II drama. In November 1939, Georg Elser's attempt to assassinate Hitler fails, and he is arrested. During his confinement, he recalls the events leading up to his plot. (Movie )">13 Minutes: The Plot to Assassinate Adolf Hitler (BBC Two Northern Ireland), World War II drama. In November 1939, Georg Elser's attempt to assassinate Hitler fails, and he is arrested. During his confinement, he recalls the events leading up to his plot. (Movie )</option>

标签: jqueryhtmljson

解决方案


尝试不要在 json 中使用单引号,而是使用这样的双引号 ("")


推荐阅读