2013-06-25 111 views
0

我正面臨着一個非常奇怪的與JSON相關的jQuery行爲。以下是我的代碼,其中我發送POST請求到發送JSON響應的servlet。我可以檢索大部分請求,但一段不短的請求的值,我得到的,同時從JSON檢索值以下錯誤:奇怪的JSON解析jQuery的問題

parsererror, SyntaxError: JSON.parse: bad control character in string literal 

但是,當我檢查JSON響應(我從日食得到它控制檯):enter link description here

本網站解析的JSON沒有提供任何錯誤!轉到網站 - >在「文本」選項卡下的textarea中複製任何JSON,然後單擊「查看器選項卡」。它會正確解析它。

以下是2-3 JSONs,爲此,jQuery的報告錯誤 -

{"topics": [{ "categoryName":"Law Crime" , "score":"90%"}],"socialTags": [{ "originalValue":"Social inequality" , "importance":"1"},{ "originalValue":"Affirmative action" , "importance":"1"},{ "originalValue":"Discrimination" , "importance":"1"},{ "originalValue":"Education policy" , "importance":"2"},{ "originalValue":"Politics" , "importance":"2"},{ "originalValue":"Ethics" , "importance":"2"},{ "originalValue":"Social philosophy" , "importance":"2"},{ "originalValue":"Same-sex marriage in Canada" , "importance":"2"},{ "originalValue":"Affirmative action in the United States" , "importance":"2"},{ "originalValue":"Same-sex marriage in the United States" , "importance":"2"},{ "originalValue":"Law Crime" , "importance":"1"}],"entities": [{ "_type":"Facility" , "name":"Supreme 
Court"},{ "_type":"Organization" , "name":"Supreme 
Court"}]} 

我已經嘗試了許多次,每次爲這些JSONs我得到同樣的錯誤的時間。

我在servlet的後端創建這些JSON。

以下是我的代碼在jQuery來檢索JSON值:

$.ajax({ 
     type: 'POST', 
     //servlet url 
     url: 'calaiscaller', 
     // parameters 
     data: {content: summary}, 
     // expected data-type of response 
     dataType: 'json', 
     // to execute when got the json result 
     success: function(jsonResponse){    
      // clear the old topic data 
      $('#topics').empty(); 
      $('#topics').append("<p class='text-left label label-info'>Topics:</p>"); 
      // add new topic data 
      $.each(jsonResponse.topics, function(){ 
       var topicData="<p><span class='text-left'>" + this.categoryName + "</span><span class='pull-right'>" + this.score + "</span></p>"; 
       $('#topics').append(topicData); 
      }); 

      // clear new social-tag data 
      $('#social-tags').empty(); 
      $('#social-tags').append("<p class='text-left label label-info'>Social Tags:</p>"); 
      // add new social-tag data 
      $.each(jsonResponse.socialTags, function(){ 
       var socialTagData="<p><span class='text-left'>" + this.originalValue + "</span><span class='pull-right'>" + this.importance + "</span></p>"; 
       $('#social-tags').append(socialTagData); 
      }); 

      // clear new entities data 
      $('#entities').empty(); 
      $('#entities').append("<p class='text-left label label-info'>Entities:</p>"); 
      // add new entities data 
      $.each(jsonResponse.entities, function(){ 
       var entitiesData="<p><span class='text-left'>" + this._type + "</span><span class='pull-right'>" + this.name + "</span></p>"; 
       $('#entities').append(entitiesData); 
      }); 

      //alert('success'); 
      // write the success status 
      $('#statusField'+uniqueId).addClass('alert alert-success pull-left'); 
      $('#statusField'+uniqueId).append('Success!'); 
     }, 

     // to execute when error 
     error: function(jqXHR, textStatus, errorThrown){ 
      //alert("error"); 
      //alert(textStatus); 
      //alert(errorThrown); 
      // print the error 
      // write the error message 
      $('#statusField'+uniqueId).addClass('alert alert-error pull-left'); 
      $('#statusField'+uniqueId).append('Error: '+textStatus+', '+errorThrown); 
     }, 

     // always executed at last whether success or error 
     complete: function(){ 
      // bring back submit button to its original state 
      $('.showinfo').button('reset'); 
      // hide the progress bar 
      $('#progress').hide(); 
      // fade in the results 
      $('#resultbox').fadeIn('slow', function(){ 

      }); 
     } 
    }); 

幫助!

+5

這個錯誤通常出現在有效的**看** JSON時,一個不可見的Unicode字符(無空間或不尋常的空白或類似的東西)以某種方式神奇地發現它的方式進入響應。 –

+1

http://jsonlint.com/給出了更好的提示JSON有什麼問題。 – Christoph

+0

這是無效的JSON在'57'線上# –

回答

2

您的原始JSON由於值中的回車符而無效。在第一個JSON字符串:

"name": "U.S. 
Supreme Court" 

在第二個字符串:

"name": "Supreme 
Court" 

如果你想有一個回車使用\n,如"Supreme\nCourt" - 但我懷疑你實際上並不希望出現這種情況,你只想要一個空間。

如果您有一個特定的JSON字符串發出錯誤,您可以在http://jsonlint.com/上驗證它。

+0

啊,這是正確的控制字符指回報..應該知道,只是沒有擊中大腦 – mcgrailm

+0

aaha!在完成replaceAll([\ n \ r],「」);它解決了!謝謝! – kunal18