jquery - 从代码“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,"'");
program[h] = program[h].replace(/\\'/g,"'");
program[h] = program[h].replace(/&/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=""[\"1670271\",\"2000: Official Top 10 (MTV Music (IRE))\",\"0\",\"0\",\"\",\"Tune in to enjoy the biggest hits of 2000 (Music)\",\"40037\",\"2\"]""><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=""[\"1643452\",\"1975: The Magnificent Masters (Sky Sports Golf)\",\"0\",\"0\",\"\",\"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,"'");
program[h] = program[h].replace(/\\'/g,"'");
program[h] = program[h].replace(/&/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=""[\"1413499\",\"13 Minutes: The Plot to Assassinate Adolf Hitler (BBC Two Northern Ireland)\",\"0\",\"0\",\"\",\"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 )\",\"519\",\"2\"]"" 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>
解决方案
尝试不要在 json 中使用单引号,而是使用这样的双引号 ("")
推荐阅读
- sas - 将 SAS Enterprise 库导入 SAS University
- reactjs - 我如何正确返回地图功能
- angular - 角度资产上传文件夹缓存?
- python - 尝试发送电子邮件时出现python AttributeError
- node.js - 如何在 nuxt 中实现 Steam 身份验证
- go - 通过引用传递接收器结构
- r - 根据另一个数据框中的列在一个数据框中应用正则表达式
- ios - iOS Swift,如何区分使用 Apple Watch 和 iPhone 的步骤?
- java - Elasticsearch 6 RestHighLevelClient:如何知道 IndexRequest 的结果何时可以读取?
- cordova - Ionic - GPS 的设备旋转