解析JSON字符串时出现语法错误 [英] Syntax error when parsing JSON string

查看:167
本文介绍了解析JSON字符串时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例JSON,其中部分网页已呈现:

I have a sample JSON with some part of my webpage rendered :

{"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抛出错误:

This string is returned with ajax call and jQuery 1.6.1 throws an error :

SyntaxError:JSON.parse:在对象的属性值之后的','或'}'

在以下代码部分中:

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。问题仍然存在。新字符串:

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 :

{"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在每个"附近添加了额外的引号,这有点奇怪。

Ok it looks, that JSOn leaving my backend is proper but dumb jQuery adds additional quotes around each '"' which is kinda odd.

推荐答案

数据是不是有效的JSON,因为 \似乎已被\ 替换。

The data is not valid JSON, since \" in strings seems to have been replaced with "\.

请联系该作者-JSON并通知他或她有充足 JSON 图书馆的html> 可用 全部 语言 平台

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字符串时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆