Javascript 解析 JSON 字符串时出现语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7921164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error when parsing JSON string
提问by mike_hornbeck
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: expected ',' or '}' after property value in object
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 库创建的,所以我想知道这怎么能在任何地方工作),现在 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 在每个 '"' 周围添加了额外的引号,这有点奇怪。
回答by Rasal Shukla
storejson= getJSonObject("@ViewBag.JsonData");
function getJSonObject(value) {
return $.parseJSON(value.replace(/"/ig, '"'));
}
回答by phihag
回答by Christiaan Nieuwlaat
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": "<input name="somename" type="text"/>"} }
i'd try to use this:
我会尝试使用这个:
{"html": { "#data": "<input name="somename" type="text"/>"} }
{"html": { "#data": "<input name="somename" type="text"/>"} }
Hope it helps...
希望能帮助到你...
回答by Andreas
回答by Tules
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 使用单引号