I have a sample JSON with some part of my webpage rendered :
我有一个示例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>"}}
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="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: 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(/"/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="somename" type="text"/>"} }
{“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(/"/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="somename" type="text"/>"} }
{“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使用单引号