當我有我的網頁的某一部分呈現一個樣本JSON:語法錯誤解析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的周圍增加了每個「"」,這是有點兒奇怪額外的報價。
我不知道,但是從它的外觀的JSON似乎無效... – Rafay