解析JSON字符串时出现语法错误 [英] Syntax error when parsing JSON string
问题描述
我有一个示例JSON,其中部分网页已呈现:
I have a sample JSON with some part of my webpage rendered :
{"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抛出错误:
This string is returned with ajax call and jQuery 1.6.1 throws an error :
SyntaxError:JSON.parse:在对象的属性值之后的','或'}'
在以下代码部分中:
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。问题仍然存在。新字符串:
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 :
{"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在每个"附近添加了额外的引号,这有点奇怪。
Ok it looks, that JSOn leaving my backend is proper but dumb jQuery adds additional quotes around each '"' which is kinda odd.
推荐答案
数据是不是有效的JSON,因为 \$字符串中的c $ c>似乎已被
\
替换。
The data is not valid JSON, since \"
in strings seems to have been replaced with "\
.
请联系该作者-JSON并通知他或她有充足 JSON 图书馆的html> 可用 全部 语言 和 平台。
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字符串时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!