即使输入正确,Python JSON 模块也会出错

标签 python json python-3.x delimiter simplejson

对于一个小组项目,我必须使用 python 导入 JSON 对象。糟糕的是,即使 JSON 字符串是正确的(使用 JSONLint 检查过),Python 仍不断给我一条错误消息。我用来测试这个的脚本如下:

import json

correct_json = """ { 
    "created_at": "Tue Feb 19 18:42:07 +0000 2013", 
    "id": 303937526471725060, 
    "id_str": "303937526471725056", 
    "text": "Batavierenrace door #Ulft: Eind april klinkt jaarlijks het startschot voor de grootste estafetteloop v... http:\\/\\/t.co\\/hijGD3pk #Enschede", 
    "source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\"> twitterfeed </a>", 
    "truncated": false, 
    "in_reply_to_status_id": null, 
    "in_reply_to_status_id_str": null, 
    "in_reply_to_user_id": null, 
    "in_reply_to_user_id_str": null, 
    "in_reply_to_screen_name": null, 
    "user": { 
        "id": 258430204, 
        "id_str": "258430204", 
        "name": "EnschedeNieuws", 
        "screen_name": "nieuws_enschede", 
        "location": "", 
        "url": "http://drimble.nl/regio/overijssel/enschede/", 
        "description": "AlhetnieuwsoverEnschede", 
        "protected": false, 
        "followers_count": 1344, 
        "friends_count": 17, 
        "listed_count": 18, 
        "created_at": "Sun Feb 27 18:17:21 +0000 2011", 
        "favourites_count": 0, 
        "utc_offset": null, 
        "time_zone": null, 
        "geo_enabled": false, 
        "verified": false, 
        "statuses_count": 20044, 
        "lang": "en", 
        "contributors_enabled": false, 
        "is_translator": false, 
        "profile_background_color": "131516", 
        "profile_background_image_url": "http://a0.twimg.com/images/themes/theme14/bg.gif", 
        "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif", 
        "profile_background_tile": true, 
        "profile_image_url": "http://a0.twimg.com/profile_images/1256839194/enschede_normal.jpg", 
        "profile_image_url_https": "https://si0.twimg.com/profile_images/1256839194/enschede_normal.jpg", 
        "profile_link_color": "009999", 
        "profile_sidebar_border_color": "EEEEEE", 
        "profile_sidebar_fill_color": "EFEFEF", 
        "profile_text_color": "333333", 
        "profile_use_background_image": true, 
        "default_profile": false, 
        "default_profile_image": false, 
        "following": null, 
        "follow_request_sent": null, 
        "notifications": null 
    }, 
    "geo": null, 
    "coordinates": null, 
    "place": null, 
    "contributors": null, 
    "retweet_count": 0, 
    "entities": { 
        "hashtags": [ 
            { 
                "text": "Ulft", 
                "indices": [ 
                    20, 
                    25 
                ] 
            }, 
            { 
                "text": "Enschede", 
                "indices": [ 
                    127, 
                    136 
                ] 
            } 
        ], 
        "urls": [ 
            { 
                "url": "http://t.co/hijGD3pk", 
                "expanded_url": "http://bit.ly/UE0MCq", 
                "display_url": "bit.ly/UE0MCq", 
                "indices": [ 
                    106, 
                    126 
                ] 
            } 
        ], 
        "user_mentions": [] 
    }, 
    "favorited": false, 
    "retweeted": false, 
    "possibly_sensitive": false 
} """

other_json = """ { \
    "foo" : 5,\
    "bar" : ["spam", "eggs"] \
} """

print(json.dumps(json.loads(correct_json), sort_keys=True, indent=4 * ' '))

很抱歉只是寻求有关如此大的 JSON 结构的帮助,但这段代码已经让我苦恼了一个多小时,我只是无法发现错误。我希望训练有素的 JSON 侦探可以帮助我。 python给我的错误是:

D:\Documenten\Dropbox>python jsontest.py
Traceback (most recent call last):
  File "jsontest.py", line 99, in <module>
    print(json.dumps(json.loads(correct_json), sort_keys=True, indent=4 * ' '))
  File "C:\Python33\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Python33\lib\json\decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 6 column 25 (char 305)

我正在使用Python 3。我可能错过了一些愚蠢的东西(这不是第一次)。提前致谢!

最佳答案

此行存在双重转义问题:

"source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\"> twitterfeed </a>", 

在此 \" 中,当您需要保持转义时,它会被解释为 "。因此,您需要对转义字符进行转义,例如 \\" 这样它就变成了 \"

所以解决办法是:

"source": "<a href=\\"http://twitterfeed.com\\" rel=\\"nofollow\\"> twitterfeed </a>",

或者,将整个字符串声明为原始字符串,如下所示:

correct_json = r""" {

但这会弄乱一些已经转义的序列。

关于即使输入正确,Python JSON 模块也会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671322/

相关文章:

javascript - 如何转换为 D3 的 JSON 格式?

python - 在 SQLAlchemy 中过滤时访问关系的 backref

java - Jython 随机字符串生成

JSON REST Api 分页页面越界或集合响应代码为空

python - 如何在python中的无向图中有效地计算三元组人口普查

Python:处理 json.load() 与 json.loads() 中的换行符

python - Pandas 对两个数字之间的列的操作

python - Excel 行到句子 Python

python - 如何根据另一列在列的第一行中填充 NA 值

python - 我们如何在 pytest 中接受一个命令行选项的多个值