2013-06-25 106 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