无法使用jQuery';解析带有HTML代码的数组;s$.parseJSON

无法使用jQuery';解析带有HTML代码的数组;s$.parseJSON,jquery,html,json,parsing,escaping,Jquery,Html,Json,Parsing,Escaping,我试图使用jQuery的$.parseJSON()将变量转换为JSON,但JSON字符串中的一个值由HTML代码组成。当我删除HTML变量并替换为“test”时,可以使用$.parseJSON解析该变量,并使用ajax成功地将其发送到php脚本,在该脚本中可以操作对象 我试图弄清楚如何将HTML代码传递到变量中并对其进行解析,以便php脚本能够访问该对象。目前,questionshtml[index]变量正在破坏脚本 我尝试过使用'questionsvalues[index].replace(“

我试图使用jQuery的$.parseJSON()将变量转换为JSON,但JSON字符串中的一个值由HTML代码组成。当我删除HTML变量并替换为“test”时,可以使用$.parseJSON解析该变量,并使用ajax成功地将其发送到php脚本,在该脚本中可以操作对象

我试图弄清楚如何将HTML代码传递到变量中并对其进行解析,以便php脚本能够访问该对象。目前,questionshtml[index]变量正在破坏脚本

我尝试过使用'questionsvalues[index].replace(“}{,“},{”);”来删除双引号和反斜杠,但脚本仍然中断。有人能提供一个解决方案,让html代码正确地准备好使用$.parseJSON()进行解析吗

JAVASCRIPT代码

var questionstype = [];
var questionshtml = [];
var questionsquestion = [];
//Store all questions, types, and html for each fieldset into 3 separate arrays
for(var i=1;i<=formpreviewid;i++)
  {
 questionsquestion.push($("#formelement_"+i + " legend").text());
 questionstype.push($("#formelement_"+i).attr("class"));
 questionshtml.push($("#formelement_"+i)[0].outerHTML);
  };
  //format values for each fieldset into values format in mysql
  var questionsvalues = [];
  var index = 0;
  for(var i=1;i<=formpreviewid;i++)
  { 
  questionsvalues.push('{"question":"'+questionsquestion[index]+'","type":"'+questionstype[index]+'","html":"'+questionshtml[index]+'"}');    
  index++;
  };

 //format mysql values into JSON format
 var questionsvaluesjson = '{"questions":['+questionsvalues+']}';
 questionsvaluesjson = $.parseJSON(questionsvaluesjson);

//convert to json
var jsonArray = JSON.stringify(questionsvaluesjson);

  $.ajax({
  type: 'POST',
  url: '/project/templates/yoo_nano2/php/saveform.php',
  data: {'formpreviewhtml': formpreviewhtml, 'jsonArray': jsonArray},
  beforeSend:function(){
    // this is where we append a loading image
   $("#formpreview").append('<div style="z-index:3000;" id="divoverlaycontainer"><div id="loaderoverlay"><div class="center" id="loader"><img src="/project/templates/yoo_nano2/images/loader.gif"></div></div></div>');
   }, success:function(data){
alert(data + "Saved form successfully.");
//$('#viewresults').empty();
$('#divoverlaycontainer').remove();
// successful request; do something with the data
//$('#viewresults').html(data);
}, error:function(){
// failed request; give feedback to user
alert("Save was unsuccessful. Please try again.");
$('#divoverlaycontainer').remove();
}
});
});
});
var-questionstype=[];
var-shtml=[];
var questionsquestion=[];
//将每个字段集的所有问题、类型和html存储到3个单独的数组中

对于(var i=1;i您是否正在转义您在json中传递的html内容?如果没有,请执行此操作并重试。另外,请验证您的json响应并确保其有效..尝试验证您的jsonI我不确定如何转义html--我正在循环页面上的元素,并使用jquery:questionsht导入代码ml.push($(“#formelement_u“+i)[0].outerHTML);--您能推荐一种在这一步进行转义的方法吗?用于转义html,同样,我们有unescape()方法将html转义回其原始格式。胡,我想我爱您。谢谢!!!工作得很好!!!:)干杯