无法解析,其中包括一个单引号JSON数据单引号、数据、JSON

2023-09-10 19:19:19 作者:青春荒唐我不负你

问题

我对我的一些JSON数据得到一个语法错误,因为它包含了单引号。 例如,我的一些数据可能看起来像这样:

拉里的数据

我已经阅读下面的文章: 在JSON响应 jQuery的单引号

和我一直在试图实施一些解决方案,但我一直没能摆脱我的语法错误。

code

在我的模型,我使用一个Lua库连接code我的数据为JSON。 该模型返回的数据是这样的:

  [{\createddatetime \:\2013年9月10日17时56分55秒\,\描述\:\李四\的电话\ ,\数字\:\72051 \,\createdname \:\转换script\",\"user\":\"23123\",\"position\":\"46\",\"id\":\"49\",\"user_id\":\"822\",\"password\":\"rwer234\"}]"
 
Excel别人发来的长数据,前面有个单引号,为啥

在我看来,我的code目前看起来是这样的:

  $。阿贾克斯({
      网址:myurl + + page_index * items_per_page的+'和; numberofrecordstograb ='+ items_per_page的+'和; viewtype = json的','startpos =?

      成功:功能(数据){

            的console.log(内部);
             对于(VAR I = 0; I< data.length;我++){
                        VAR deviceobj =数据[I]
                        newcontent = newcontent +&其中; TR>中;
                        newcontent = newcontent +'&其中; TD>';

                        //添加编辑超链接
                        如果($(#editdevicesettings)。VAL()==真){
                            变种临时= $(#editlinkpath)VAL()。
                            newcontent = newcontent + temp.replace(XXX,deviceobj [DEVICE_ID])+'&安培; NBSP;&安培; NBSP;';
                        }

                        //添加删除超级链接
                        如果($(#deletedevice)。VAL()==真){
                            变种临时= $(#deletelinkpath)VAL()。
                            newcontent = newcontent + temp.replace(XXX,deviceobj [DEVICE_ID]);
                        }
                        newcontent = newcontent +'< / TD>';

                        newcontent = newcontent +'&其中; TD>' + deviceobj [数量] +'< / TD>';
                        newcontent = newcontent +'&其中;&其中; TD>' + deviceobj [用户] +'< / TD>';
                        newcontent = newcontent +'&其中;&其中; TD>' + deviceobj [密码] +'< / TD>';
                        如果(deviceobj [名称]){
                              newcontent = newcontent +'&其中; TD>' + deviceobj [名称] +'< / TD>';
                        }
                        其他  {
                             newcontent = newcontent +'&其中; TD>&安培; NBSP;&所述; / TD>';
                        }
                        newcontent = newcontent +'&其中; TD>' +反向转义(deviceobj [说明])+'< / TD>';
                        newcontent = newcontent +&所述; / TR>中;
                } //结束
                //替换旧的内容与新内容
                $('#信息搜索结果)HTML(newcontent);
            如果} //结束

      },
      错误:函数(请求,textStatus,errorThrown){
        执行console.log(textStatus);
        的console.log('========');
        执行console.log(要求);

      },
      完成:功能(请求,textStatus){//,了解更多信息
        //alert(request.responseText);
        执行console.log(textStatus);
      }
    });
 

但是我还是对这个特定记录解析错误。

任何建议将是AP preciated。 谢谢。

修改1

我已经改变了我的逻辑,这样当它失败时,它打印出request.responseText到控制台。这里是什么样子:

 [{\createddatetime \:\2013年9月10日17点56分55秒\,\描述\:\李四\的电话\,\数字\:\72051 \,\createdname \:\转换script\",\"user\":\"28567\",\"position\":\"46\",\"id\":\"49\",\"user_id\":\"822\",\"password\":\"rwer234\"}]"
 

撇号还是逃脱了。

编辑2

下面就是我的code看起来像在服务器端(即模型):

  get_device_records =功能(ajaxdata)
   当地结果= list_devices(零,假的,ajaxdata.startpos,ajaxdata.numberofrecordstograb)
   回报results.value
结束
 

解决方案

看起来你正在做的双重序列化在服务器端。偏偏例如,如果你的Web框架自动串行化返回的对象,但你让你的对象一个额外的明确的,不必要的连载电话。您的code工作没有案件的事实证明了这一点:jQuery让(因为数据类型)一种自动解析,那么你运行另一个parseJSON。这不应该工作:) 修正你的序列化在服务器端,并删除您的成功方法,不必要的parseJSON电话。每一个其他的解决办法只是一个解决方法不是一个真正的解决。

Problem

I'm getting a parse error on some of my json data, because it includes single quotes. For example, some of my data could look like this:

"Larry's data"

I've read the following article: jQuery single quote in JSON response

and I've been trying to implement some of the solutions but I haven't been able to get rid of my parse error.

Code

In my model, I'm using a lua library to encode my data as json. The model returns data that looks like this:

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

In my view, my code currently looks like this:

  $.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);
      }
    });

But I still get the parse error on this particular record.

Any suggestions would be appreciated. Thanks.

EDIT 1

I've changed my logic so that when it fails, it print out "request.responseText" into the console. Here's what it looks like:

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

The apostrophe is still escaped.

EDIT 2

Here's what my code looks like on the server side (aka. in the model):

get_device_records = function(ajaxdata)
   local results = list_devices(nil,false,ajaxdata.startpos, ajaxdata.numberofrecordstograb)
   return results.value
end

解决方案

Looks like you are doing double serialisation on the server side. Happens for example if your web framework automatically serialises the returned object but you make an extra explicit, unnecessary serialise call on your object. The fact that your code works with no ' cases proves this: jquery makes one parse automatically (because of the dataType) then you run another parseJSON. That shouldn't work :) Fix your serialisation on the server side and remove the unnecessary parseJSON call from your success method. Every other solution is just a workaround not a real fix.

 
精彩推荐
图片推荐