我有一个示例JSON,其中部分网页已呈现:
{"html": {"#data": "\n<h2>Data</h2>\n<div class="\"manufacturer-image\"">\n \n</div>\n
<form action="\"/manage/update-manufacturer-data/3\"" method="\"post\"">\n \n
<div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_name\"">Nazwa</label>:\n
</div>\n \n \n <div class="\"error\"">\n
<input id="\"id_name\"" name="\"name\"" maxlength="50" type="\"text\"">\n
<ul class="\"errorlist\""><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n
<div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_image\"">Zdjecie</label>:\n
</div>\n \n \n <div>\n <input name="\"image\"" id="\"id_image\"" type="\"file\"">\n
</div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n
<label for="\"id_description\"">Opis</label>:\n </div>\n \n \n <div>\n
<textarea id="\"id_description\"" rows="10" cols="40" name="\"description\""></textarea>\n </div>\n \n
</div>\n \n <div class="\"buttons\"">\n <input class="\"ajax-save-button" button\"="" type="\"submit\"">\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="manufacturer-image">\n \n</div>\n<form action="/manage/update-manufacturer-data/4" method="post">\n \n <div class="field">\n <div class="label">\n <label for="id_name">Nazwa</label>:\n </div>\n \n \n <div class="error">\n <input id="id_name" type="text" name="name" maxlength="50" />\n <ul class="errorlist"><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n <div class="field">\n <div class="label">\n <label for="id_image">Zdjecie</label>:\n </div>\n \n \n <div>\n <input type="file" name="image" id="id_image" />\n </div>\n \n </div>\n\n <div class="field">\n <div class="label">\n <label for="id_description">Opis</label>:\n </div>\n \n \n <div>\n <textarea id="id_description" rows="10" cols="40" name="description"></textarea>\n </div>\n \n </div>\n \n <div class="buttons">\n <input type="submit" class="ajax-save-button button" />\n </div>\n</form>"}]}
EDIT2: 好吧,看起来,JSOn离开我的后端是正确的但是愚蠢的jQuery在每个'''附近添加了额外的引号,这有点奇怪。
答案 0 :(得分:10)
storejson= getJSonObject("@ViewBag.JsonData");
function getJSonObject(value) {
return $.parseJSON(value.replace(/"/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="somename" type="text"/>"} }
希望它有所帮助...
答案 3 :(得分:2)
JSON = Javascript Object Notation,因此"#data"
属性的值是无效字符串。
您可以验证您的JSON here
答案 4 :(得分:-1)
我认为你的HTML代码本身可能使用相同的双引号,所以JSON认为它是一个属性值,尝试对所有HTML使用单引号