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

时间:2022-10-17 09:01:08

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:在对象中的属性值之后预期','或'}'

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 lib创建的,所以我想知道它是如何在任何地方工作的)现在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在每个'''附近添加了额外的引号,这有点奇怪。

5 个解决方案

#1


8  

The data is not valid 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 plenty of JSON libraries available for all languages and platforms.

联系那个所谓的JSON的作者并通知他或她有很多JSON库可用于所有语言和平台。

#2


10  

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

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

#3


3  

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”:“”}}

i'd try to use this:

我试着用这个:

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

{“html”:{“#data”:“ <输入名称=" somename" type=""" text" />”}}

Hope it helps...

希望能帮助到你...

#4


2  

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

#5


-1  

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使用单引号

#1


8  

The data is not valid 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 plenty of JSON libraries available for all languages and platforms.

联系那个所谓的JSON的作者并通知他或她有很多JSON库可用于所有语言和平台。

#2


10  

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

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

#3


3  

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”:“”}}

i'd try to use this:

我试着用这个:

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

{“html”:{“#data”:“ <输入名称=" somename" type=""" text" />”}}

Hope it helps...

希望能帮助到你...

#4


2  

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

#5


-1  

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使用单引号