[英]unable to parse json data that includes a single quote
问题
我在某些json数据上遇到解析错误,因为它包含单引号。 例如,我的一些数据可能如下所示:
“拉里的数据”
我已阅读以下文章: JSON响应中的jQuery单引号
并且我一直在尝试实现一些解决方案,但是我一直无法摆脱解析错误。
码
在我的模型中,我使用lua库将数据编码为json。 该模型返回的数据如下所示:
[{\"createddatetime\":\"2013-09-10 17:56:55\",\"description\":\"John Doe\'s phone\",\"number\":\"72051\",\"createdname\":\"conversion script\",\"user\":\"23123\",\"position\":\"46\",\"id\":\"49\",\"user_id\":\"822\",\"password\":\"rwer234\"}]"
在我看来,我的代码当前如下所示:
$.ajax({
url:myurl + '?startpos=' + page_index * items_per_page + '&numberofrecordstograb=' + items_per_page + '&viewtype=json',
success: function(data){
console.log('inside');
for(var i=0;i<data.length;i++) {
var deviceobj = data[i];
newcontent = newcontent + "<TR>";
newcontent=newcontent + '<TD>';
//add EDIT hyperlink
if ($("#editdevicesettings").val() == "true") {
var temp = $("#editlinkpath").val();
newcontent=newcontent + temp.replace("xxx",deviceobj["device_id"]) + ' ';
}
//add DELETE hyperlink
if ($("#deletedevice").val() == "true") {
var temp = $("#deletelinkpath").val();
newcontent=newcontent + temp.replace("xxx",deviceobj["device_id"]);
}
newcontent=newcontent + '</TD>';
newcontent=newcontent + '<TD>' + deviceobj["number"] +'</TD>';
newcontent=newcontent + '<<TD>' + deviceobj["user"] + '</TD>';
newcontent=newcontent + '<<TD>' + deviceobj["password"] + '</TD>';
if (deviceobj["name"]) {
newcontent=newcontent + '<TD>' + deviceobj["name"] + '</TD>';
}
else {
newcontent=newcontent + '<TD> </TD>';
}
newcontent=newcontent + '<TD>' + unescape(deviceobj["description"]) + '</TD>';
newcontent = newcontent + "</TR>";
}// end for
// Replace old content with new content
$('#Searchresult').html(newcontent);
}//end if
},
error: function(request, textStatus, errorThrown) {
console.log(textStatus);
console.log('========');
console.log(request);
},
complete: function(request, textStatus) { //for additional info
//alert(request.responseText);
console.log(textStatus);
}
});
但是我仍然在此特定记录上遇到解析错误。
任何建议,将不胜感激。 谢谢。
编辑1
我更改了逻辑,以使其在失败时将“ request.responseText”输出到控制台。 看起来是这样的:
"[{\"createddatetime\":\"2013-09-10 17:56:55\",\"description\":\"John Doe\'s phone\",\"number\":\"72051\",\"createdname\":\"conversion script\",\"user\":\"28567\",\"position\":\"46\",\"id\":\"49\",\"user_id\":\"822\",\"password\":\"rwer234\"}]"
撇号仍然有效。
编辑2
这是我的代码在服务器端(在模型中)的样子:
get_device_records = function(ajaxdata)
local results = list_devices(nil,false,ajaxdata.startpos, ajaxdata.numberofrecordstograb)
return results.value
end
看起来您正在服务器端执行双重序列化。 例如,如果您的Web框架自动序列化了返回的对象,但是您对对象进行了额外的显式,不必要的序列化调用,则会发生这种情况。 您的代码没有大小写情况的事实证明了这一点:jquery自动进行一个解析(由于dataType),然后运行另一个parseJSON。 那不应该工作:)在服务器端修复序列化,并从成功方法中删除不必要的parseJSON调用。 其他所有解决方案都只是一种解决方法,而不是真正的解决方案。
尝试更换
data = data.replace("\\'", "'");
与
data = data.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
我在解析JSON数据时遇到类似的问题,而另一个答案中的这段代码解决了它
这是json4lua 0.9.40库的lua json.encode函数中的错误。 错误地转义了单引号。 0.9.50中对此进行了更正:
只需从json响应中取出\\\\。 我的意思是按原样传递单引号,如下所示:
[{\"createddatetime\":\"2013-09-10 17:56:55\",\"description\":\"John Doe's phone\",\"number\":\"72051\",\"createdname\":\"conversion script\",\"user\":\"23123\",\"position\":\"46\",\"id\":\"49\",\"user_id\":\"822\",\"password\":\"rwer234\"}]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.