簡體   English   中英

無法解析包含單引號的json數據

[英]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"]) + '&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自動進行一個解析(由於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中對此進行了更正:

https://github.com/craigmj/json4lua

只需從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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM