javascript - 无法解析包含单引号的 json 数据

标签 javascript jquery ajax json

问题

我的一些 json 数据出现解析错误,因为它包含单引号。 例如,我的一些数据可能如下所示:

“拉里的数据”

我已阅读以下文章: jQuery single quote in JSON response

我一直在尝试实现一些解决方案,但我一直无法摆脱我的解析错误。

代码

在我的模型中,我使用 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"]) + '&nbsp;&nbsp;';
                        } 

                        //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>&nbsp;</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 自动进行一个解析(由于数据类型),然后您运行另一个 parseJSON。那不应该工作:) 修复服务器端的序列化,并从成功方法中删除不必要的 parseJSON 调用。所有其他解决方案都只是一种解决方法,而不是真正的解决方法。

关于javascript - 无法解析包含单引号的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303617/

相关文章:

javascript - 为什么 Node Javascript 中大型 Array 对象的内存分配行为会发生变化? ( docker 容器)

javascript - Dropbox + Chrome 扩展 Oauth 接收器

javascript - 主屏幕应用程序(android)facebook登录打开浏览器

javascript - 如何让动画无限循环运行

javascript - 如何对与 DOM 元素交互的 Javascript 代码进行单元测试

javascript - 使用 jQuery 获取 'ping' URL

javascript - 如果客户端验证文本相同并显示,如何不显示服务器端验证消息?

jquery - Ajax 请求不打印 html 响应

php - 通过 Ajax 在 Javascript 到 PHP 之间保留真正的 ARRAY OBJECT?

jquery - JSON ajax SyntaxError : JSON. 解析:意外字符