Javascript 解析 JSON 字符串时出现语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7921164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:04:50  来源:igfitidea点击:

Syntax error when parsing JSON string

javascriptjqueryjsonparsing

提问by mike_hornbeck

I have a sample JSON with some part of my webpage rendered :

我有一个示例 JSON,其中呈现了我的网页的某些部分:

{"html": {"#data": "\n<h2>Data</h2>\n<div class="\&quot;manufacturer-image\&quot;">\n \n</div>\n
<form action="\&quot;/manage/update-manufacturer-data/3\&quot;" method="\&quot;post\&quot;">\n \n 
<div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n <label for="\&quot;id_name\&quot;">Nazwa</label>:\n 
</div>\n \n \n <div class="\&quot;error\&quot;">\n 
<input id="\&quot;id_name\&quot;" name="\&quot;name\&quot;" maxlength="50" type="\&quot;text\&quot;">\n 
<ul class="\&quot;errorlist\&quot;"><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n 
<div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n <label for="\&quot;id_image\&quot;">Zdjecie</label>:\n 
</div>\n \n \n <div>\n <input name="\&quot;image\&quot;" id="\&quot;id_image\&quot;" type="\&quot;file\&quot;">\n 
</div>\n \n </div>\n\n <div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n 
<label for="\&quot;id_description\&quot;">Opis</label>:\n </div>\n \n \n <div>\n 
<textarea id="\&quot;id_description\&quot;" rows="10" cols="40" name="\&quot;description\&quot;"></textarea>\n </div>\n \n 
</div>\n \n <div class="\&quot;buttons\&quot;">\n <input class="\&quot;ajax-save-button" button\"="" type="\&quot;submit\&quot;">\n 
</div>\n</form>"}}

This string is returned with ajax call and jQuery 1.6.1 throws an error :

此字符串通过 ajax 调用返回,jQuery 1.6.1 抛出错误:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object

SyntaxError: JSON.parse: expected ',' or '}' after property value in object

in the following part of the code :

在代码的以下部分:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }
    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        console.warn('data: ', data);
        var ret;
        try{
            ret = window.JSON.parse(data);
        } catch(e){
            ret = {};
            console.warn(e);
        }
        return ret;
        //return window.JSON.parse( data );
    }

What am I missing here ?

我在这里错过了什么?



EDIT:

编辑:

I have parsed through the previous 'json' (which by the way was created with python's simplejson lib, so I wonder how can this be working anywhere) and now jsonlint shows, that I have proper JSON. Still the problem remains the same. The new string :

我已经解析了之前的“json”(顺便说一下,它是用 python 的 simplejson 库创建的,所以我想知道这怎么能在任何地方工作),现在 jsonlint 显示,我有正确的 JSON。问题仍然是一样的。新字符串:

{"html": [{"#data": "\n<h2>Data</h2>\n<div class=&quot;manufacturer-image&quot;>\n    \n</div>\n<form action=&quot;/manage/update-manufacturer-data/4&quot; method=&quot;post&quot;>\n        \n    <div class=&quot;field&quot;>\n        <div class=&quot;label&quot;>\n            <label for=&quot;id_name&quot;>Nazwa</label>:\n        </div>\n        \n        \n            <div class=&quot;error&quot;>\n                <input id=&quot;id_name&quot; type=&quot;text&quot; name=&quot;name&quot; maxlength=&quot;50&quot; />\n                <ul class=&quot;errorlist&quot;><li>Pole wymagane</li></ul>\n            </div>\n        \n    </div>\n\n    <div class=&quot;field&quot;>\n        <div class=&quot;label&quot;>\n            <label for=&quot;id_image&quot;>Zdjecie</label>:\n        </div>\n        \n        \n            <div>\n                <input type=&quot;file&quot; name=&quot;image&quot; id=&quot;id_image&quot; />\n            </div>\n        \n    </div>\n\n    <div class=&quot;field&quot;>\n        <div class=&quot;label&quot;>\n            <label for=&quot;id_description&quot;>Opis</label>:\n        </div>\n        \n        \n            <div>\n                <textarea id=&quot;id_description&quot; rows=&quot;10&quot; cols=&quot;40&quot; name=&quot;description&quot;></textarea>\n            </div>\n        \n    </div>\n  \n    <div class=&quot;buttons&quot;>\n        <input type=&quot;submit&quot; class=&quot;ajax-save-button button&quot; />\n    </div>\n</form>"}]}

EDIT2: Ok it looks, that JSOn leaving my backend is proper but dumb jQuery adds additional quotes around each '"' which is kinda odd.

EDIT2:好吧,看起来,JSOn 离开我的后端是正确的,但愚蠢的 jQuery 在每个 '"' 周围添加了额外的引号,这有点奇怪。

回答by Rasal Shukla

storejson= getJSonObject("@ViewBag.JsonData");

function getJSonObject(value) {
    return $.parseJSON(value.replace(/&quot;/ig, '"'));
}

回答by phihag

The data is notvalid JSON, since \"in strings seems to have been replaced with "\.

数据不是有效的 JSON,因为\"字符串中似乎已替换为"\.

Contact the author of that supposedly-JSON and notify him or her that there are plentyofJSONlibrariesavailableforalllanguagesandplatforms.

联系那个所谓的 JSON 的作者,并通知他或她有大量适用所有语言平台JSON

回答by Christiaan Nieuwlaat

I might be mistaken here but I think it's due to the JSON data itself it doesn't work. The JSON parser probably chokes on the double-quotes in the HTML contained in the JSON'ed string variable. I think it will work when you pre-process the HTML string before outputting the JSON data so you change the double-quotes into something else. ( and do the other way around after parsing the JSON data )

我可能在这里弄错了,但我认为这是由于 JSON 数据本身它不起作用。JSON 解析器可能会被包含在 JSON 字符串变量中的 HTML 中的双引号阻塞。我认为当您在输出 JSON 数据之前预处理 HTML 字符串时它会起作用,因此您将双引号更改为其他内容。(并在解析 JSON 数据后反过来做)

An example might clarify a bit:

一个例子可能会澄清一点:

instead of this: {"html": { "#data": "<input name="somename" type="text"/>"} }

而不是这个: {"html": { "#data": "<input name="somename" type="text"/>"} }

i'd try to use this:

我会尝试使用这个:

{"html": { "#data": "<input name=&quot;somename&quot; type=&quot;text&quot;/>"} }

{"html": { "#data": "<input name=&quot;somename&quot; type=&quot;text&quot;/>"} }

Hope it helps...

希望能帮助到你...

回答by Andreas

JSON = Javascript Object Notation, therefor the value of the "#data"property is an invalid string.

JSON = Javascript Object Notation,因此"#data"属性值是无效字符串。

You can validate your JSON here

您可以在此处验证您的 JSON

回答by Tules

I think it's probably the fact that you are using the same double quotes in your HTML code itself so the JSON thinks its a property value, try using single quotes for all the HTML

我认为这可能是因为您在 HTML 代码本身中使用了相同的双引号,因此 JSON 认为它是一个属性值,请尝试对所有 HTML 使用单引号