从代码“ JSON输入意外结束”中获取以下错误消息

时间:2019-06-14 07:39:56

标签: jquery html 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>

1 个答案:

答案 0 :(得分:0)

尝试不对json使用单引号,而是使用像这样的双引号(“”)