gpt4 book ai didi

javascript - 解析 JSON 字符串时出现语法错误

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:47 25 4
gpt4 key购买 nike

我有一个示例 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: 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 库创建的,所以我想知道它如何在任何地方工作),现在 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>"}]}

编辑2:好的,看起来 JSOn 离开我的后端是正确的,但是愚蠢的 jQuery 在每个 '"' 周围添加了额外的引号,这有点奇怪。

最佳答案

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

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

关于javascript - 解析 JSON 字符串时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7921164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com