javascript - jQuery 奇怪的 json 解析问题

标签 javascript jquery json

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

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

但是当我检查此站点上的 JSON 响应(我从 eclipse 控制台获取它)时:enter link description here

该网站解析 JSON 时没有给出任何错误!转到站点 -> 复制“文本”选项卡下文本区域中的任何 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(){

            });
        }
    });

救命!

最佳答案

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

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

在第二个字符串中:

"name": "Supreme
Court"

如果您想要回车,请使用 \n,如 "Supreme\nCourt" - 但我怀疑您实际上并不想要这样,您只是想要一个空间。

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

关于javascript - jQuery 奇怪的 json 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17297078/

相关文章:

json - 在 Swift 上访问变量 do-catch 语句

java - 检查 json 是否包含字符串值

javascript - 如何对对象的 JS 对象进行排序?

javascript - 从 Gist 调用函数

javascript - HTML5 Canvas : Get Event when drawing is finished

javascript - 使用 JavaScript 将 5'5"表示法中的英尺转换为厘米

javascript - 如何检查动态生成的 radio 输入值

javascript - 附加新元素后功能不起作用

jquery - 如何使用 jQuery 将一个选择框的选定文本和值传输到另一个选择框?

jquery - ie9 fallback for bootstrap carousel fade variation