值错误:期望属性名称:json.load() 上的第 1 行第 2 列(字符 1)


with open('tmp.txt', 'r') as f:
    json.load(f)

上面的代码段在文本文件tmp.txt上执行

,其内容如下所示:

{u'Messages': [{u'Body': '{"follow_request_sent": false, "has_extended_profile": false, "profile_use_background_image": true, "default_profile_image": false, "id": 1000505012, "verified": false, "translator_type": "none", "profile_text_color": "333333", "profile_sidebar_fill_color": "DDEEF6", "entities": {"url": {"urls": []}, "description": {"urls": []}}, "followers_count": 1994, "profile_sidebar_border_color": "C0DEED", "id_str": "1000505012", "profile_background_color": "C0DEED", "listed_count": 61, "status": {"contributors": null, "truncated": false, "text": "RT @UWT_UK: A sandstorm hit during a distribution of winter supplies to families displaced from Tal Afar, Iraq. Three thousand families acr\u2026", "is_quote_status": false, "in_reply_to_status_id": null, "id": 935144103498735616, "favorite_count": 0, "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [{"id": 227625530, "indices": [3, 10], "id_str": "227625530", "screen_name": "UWT_UK", "name": "Ummah Welfare Trust"}], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 24, "id_str": "935144103498735616", "favorited": false, "retweeted_status": {"contributors": null, "truncated": true, "text": "A sandstorm hit during a distribution of winter supplies to families displaced from Tal Afar, Iraq. Three thousand\u2026", "is_quote_status": false, "in_reply_to_status_id": null, "id": 935142116078047232, "favorite_count": 33, "source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 24, "id_str": "935142116078047232", "favorited": false, "geo": null, "in_reply_to_user_id_str": null, "possibly_sensitive": false, "lang": "en", "created_at": "Mon Nov 27 13:43:49 +0000 2017", "in_reply_to_status_id_str": null, "place": null}, "geo": null, "in_reply_to_user_id_str": null, "lang": "en", "created_at": "Mon Nov 27 13:51:43 +0000 2017", "in_reply_to_status_id_str": null, "place": null}, "is_translation_enabled": false, "utc_offset": 0, "statuses_count": 48823, "description": "Today, I have perfected your religion for you, and\nhave completed My blessing upon you, and chosen Islam as Din (religion and a way of life) for you. Al Quran", "friends_count": 2456, "location": "Bolton, England attablig.com", "profile_link_color": "1DA1F2", "screen_name": "MohsinTai", "lang": "en", "profile_background_tile": false, "favourites_count": 57896, "name": "studentofknowledge", "notifications": false, "created_at": "Mon Dec 10 00:06:03 +0000 2012", "contributors_enabled": false, "time_zone": "London", "protected": false, "default_profile": true, "is_translator": false}', u'ReceiptHandle': 'AQEBvFJHawi3wr3rDD0tJp5gI/96AaZk4YyYGmT0subNPewtON1TWi6mGAKAKENZKYrdmpG53CwwWCn7OHdiG0gg2WAFF+SFF5dJNum5RD2NiM4WJptrKaTAzD++4sxJ7b5ZSFe6pNkBqiBKZ8HxRJ+WU2iMcqtGBTWi7hA1zqU3xv03JauYcW//sQ4nFZetgsen8MLpkE+D/N+fchydlAf0dB5+gJvCi5GTaIqqeFw3/4OgG1GzHoGsH6jzTsWxJ+P5xeAdAzmf75oDQC3z4GumjX0KVm9lEtjSonotxfrxoLThZoH0nJPJSe+4ICwAQEiOtMXr05z/37LLt1rUGnGSdWvP4kliPq1Y/0VJJK9zRfsyhnVaQjgo4piGxORSAzbszhXzxaUpgNxZIc35HT1chw==', u'MD5OfBody': '55e20d43e4116711c04f3c0cfc2091f4', u'MD5OfMessageAttributes': 'ae9dba0e09b0e1c323fa29b5f052fc50', u'MessageId': 'a0c29b32-dd11-478a-879c-9e933e65373a', u'MessageAttributes': {'Organization_handler': {u'DataType': 'String', u'StringValue': 'AKF_Social'}}}}

执行上述代码段时,会引发以下错误: ValueError: Expecting property name: line 1 column 2(char 1)

我哪里做错了?

正如您使用 json 解析工具所看到的,该文件不是有效的 json 文件。

错误消息是关于文件的第二个字符:u'Messages'中的u

'Messages'也是无效的。

这是"Messages".

'Body': '{"......{ ???之前'是什么

此文件不是有效的 JSON 文件,因此json.load无法加载它。
JSON 文件中的所有键都必须字符串
在您的情况下,某些键具有您需要删除的前缀字母 u

相关内容

最新更新