2011-10-27 199 views
3

当我有我的网页的某一部分呈现一个样本JSON:语法错误解析JSON字符串

{"html": {"#data": "\n<h2>Data</h2>\n<div class="\&quot;manufacturer-image\&quot;">\n \n</div>\n 
<form action="\&quot;/manage/update-manufacturer-data/3\&quot;" method="\&quot;post\&quot;">\n \n 
<div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n <label for="\&quot;id_name\&quot;">Nazwa</label>:\n 
</div>\n \n \n <div class="\&quot;error\&quot;">\n 
<input id="\&quot;id_name\&quot;" name="\&quot;name\&quot;" maxlength="50" type="\&quot;text\&quot;">\n 
<ul class="\&quot;errorlist\&quot;"><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n 
<div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n <label for="\&quot;id_image\&quot;">Zdjecie</label>:\n 
</div>\n \n \n <div>\n <input name="\&quot;image\&quot;" id="\&quot;id_image\&quot;" type="\&quot;file\&quot;">\n 
</div>\n \n </div>\n\n <div class="\&quot;field\&quot;">\n <div class="\&quot;label\&quot;">\n 
<label for="\&quot;id_description\&quot;">Opis</label>:\n </div>\n \n \n <div>\n 
<textarea id="\&quot;id_description\&quot;" rows="10" cols="40" name="\&quot;description\&quot;"></textarea>\n </div>\n \n 
</div>\n \n <div class="\&quot;buttons\&quot;">\n <input class="\&quot;ajax-save-button" button\"="" type="\&quot;submit\&quot;">\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=&quot;manufacturer-image&quot;>\n \n</div>\n<form action=&quot;/manage/update-manufacturer-data/4&quot; method=&quot;post&quot;>\n  \n <div class=&quot;field&quot;>\n  <div class=&quot;label&quot;>\n   <label for=&quot;id_name&quot;>Nazwa</label>:\n  </div>\n  \n  \n   <div class=&quot;error&quot;>\n    <input id=&quot;id_name&quot; type=&quot;text&quot; name=&quot;name&quot; maxlength=&quot;50&quot; />\n    <ul class=&quot;errorlist&quot;><li>Pole wymagane</li></ul>\n   </div>\n  \n </div>\n\n <div class=&quot;field&quot;>\n  <div class=&quot;label&quot;>\n   <label for=&quot;id_image&quot;>Zdjecie</label>:\n  </div>\n  \n  \n   <div>\n    <input type=&quot;file&quot; name=&quot;image&quot; id=&quot;id_image&quot; />\n   </div>\n  \n </div>\n\n <div class=&quot;field&quot;>\n  <div class=&quot;label&quot;>\n   <label for=&quot;id_description&quot;>Opis</label>:\n  </div>\n  \n  \n   <div>\n    <textarea id=&quot;id_description&quot; rows=&quot;10&quot; cols=&quot;40&quot; name=&quot;description&quot;></textarea>\n   </div>\n  \n </div>\n \n <div class=&quot;buttons&quot;>\n  <input type=&quot;submit&quot; class=&quot;ajax-save-button button&quot; />\n </div>\n</form>"}]} 

EDIT2: 好看起来,这JSON离开我的后台是正确的,但愚蠢的jQuery的周围增加了每个“"”,这是有点儿奇怪额外的报价。

+0

我不知道,但是从它的外观的JSON似乎无效... – Rafay

回答

7

数据是not有效的JSON,因为\"中的字符串似乎已被替换为"\

联系,理应-JSON的作者,并通知他或她有plentyofJSONlibrariesavailableforalllanguagesandplatforms

+4

您可以随时使用:http://jsonlint.com/检查如果它是有效的JSON。另外,以上是无效的JSON。 –

+0

@JamieRRytlewski好点。不幸的是,我找不到一种方法来生成指向特定验证的链接,所以我在ideone上使用了JavaScript模式。 – phihag

0

我认为这可能是事实,你正在使用这样的JSON认为在你的HTML代码本身相同双引号的属性值,尝试使用单引号所有的HTML

2

我可能会在这里误,但我认为这是由于JSON数据本身不起作用。 JSON解析器可能会在包含在JSON中的字符串变量中的HTML中的双引号上扼杀。我认为它会在输出JSON数据之前预处理HTML字符串时起作用,因此您可以将双引号更改为其他字符。 (和做其他方式解析JSON数据后)

一个例子可以澄清一点:

,而不是这样的: {"html": { "#data": "<input name="somename" type="text"/>"} }

我会尝试使用此:

{"html": { "#data": "<input name=&quot;somename&quot; type=&quot;text&quot;/>"} }

希望它可以帮助...

+1

这对我有用。为了简化引用,我使用了'JSON.stringify(foo).replace(/ \\“/ g,'\\\\'')' –

1

JSON = JavaScript对象Notati因此,"#data"属性的值是无效的字符串。

您可以验证您的JSON here

8
storejson= getJSonObject("@ViewBag.JsonData"); 

function getJSonObject(value) { 
    return $.parseJSON(value.replace(/&quot;/ig, '"')); 
}