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

时间:2011-10-27 19:19:01

标签: javascript jquery json parsing

我有一个示例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>"}}

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

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

代码的以下部分:

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 );
    }

我在这里缺少什么?


编辑:

我已经解析了之前的'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: 好吧,看起来,JSOn离开我的后端是正确的但是愚蠢的jQuery在每个'''附近添加了额外的引号,这有点奇怪。

5 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:8)

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

联系该所谓JSON的作者,并通知他或她plenty of JSON libraries available for { {3}} all languages and

答案 2 :(得分:3)

我可能会在这里弄错,但我认为这是由于JSON数据本身不起作用。 JSON解析器可能会扼杀JSON'ed字符串变量中包含的HTML中的双引号。我认为在输出JSON数据之前预处理HTML字符串时,它会起作用,因此您可以将双引号更改为其他内容。 (并在解析JSON数据后执行相反的操作)

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

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

我试着用这个:

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

希望它有所帮助...

答案 3 :(得分:2)

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

您可以验证您的JSON here

答案 4 :(得分:-1)

我认为你的HTML代码本身可能使用相同的双引号,所以JSON认为它是一个属性值,尝试对所有HTML使用单引号