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

时间:2013-10-10 18:37:47

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

我已经改变了我的逻辑,以便在失败时打印出#34; request.responseText&#34;进入控制台。这是它的样子:

"[{\"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

4 个答案:

答案 0 :(得分:6)

看起来你正在服务器端进行双序列化。例如,如果您的Web框架自动序列化返回的对象,但您对对象进行了额外的显式,不必要的序列化调用,则会发生这种情况。你的代码没有'case'的事实证明了这一点:jquery自动解析一个(因为dataType)然后你运行另一个parseJSON。那应该不行:) 修复服务器端的序列化,并从success方法中删除不必要的parseJSON调用。其他每个解决方案都只是一种解决方法而不是真正的解决方案。

答案 1 :(得分:4)

尝试替换

data = data.replace("\\'", "'");

data = data.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');

我在解析JSON数据方面遇到了类似的问题,而这段代码from another answer解决了它

答案 2 :(得分:1)

这是json4lua 0.9.40库的lua json.encode函数中的一个错误。它错误地逃脱单引号。这在0.9.50:

中得到纠正

https://github.com/craigmj/json4lua

答案 3 :(得分:0)

从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\"}]"