奇怪的json解析jQuery的问题

时间:2013-06-25 12:15:34

标签: javascript jquery json

我正面临一个与json相关的非常奇怪的jQuery行为。以下是我的代码,其中我向发送JSON响应的servlet发送POST请求。我能够检索大多数请求的值,但有些请求,我从JSON检索值时收到以下错误:

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

但是当我在这个网站上检查JSON响应(我是从eclipse控制台获取它)时:enter link description here

此站点解析JSON而不会出现任何错误!转到网站 - >在“文本”标签下的textarea中复制任何JSON,然后点击“查看器标签”。它会正确解析它。

以下是2-3个JSON,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"}]}

我已经多次尝试过这些JSON,但我得到了同样的错误。

我在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(){

            });
        }
    });

帮助!

1 个答案:

答案 0 :(得分:2)

由于值中的回车,原始JSON无效。在第一个JSON字符串中:

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

在第二个字符串中:

"name": "Supreme
Court"

如果你想要回车使用\n,就像"Supreme\nCourt"一样 - 但我怀疑你实际上并不想要那个,你只需要一个空格。

如果您有一个错误的特定JSON字符串,您可以在http://jsonlint.com/验证它。

相关问题