当前位置: 首页 > 知识库问答 >
问题:

我无法在python上加载json文件(JSONDecodeError:额外数据)[重复]

江英华
2023-03-14

我正在尝试使用JSON加载一些JSON数据。load(),但我一直收到一条错误消息,我不知道如何修复它。

这是json文件的一部分示例,有已删除的推文(以{"delete":{开头)和已创建的推文(以{"created_at":开头):

{"delete":{"status":{"id":509743302972043264,"id_str":"509743302972043264","user_id":1366812392,"user_id_str":"1366812392"},"timestamp_ms":"1410368494532"}}
{"delete":{"status":{"id":64472572007428096,"id_str":"64472572007428096","user_id":31473446,"user_id_str":"31473446"},"timestamp_ms":"1410368494565"}}
{"created_at":"Wed Sep 10 17:01:34 +0000 2014","id":509748529070616576,"id_str":"509748529070616576","text":"Metin \u015eent\u00fcrk 
Twitterda @metinsenturk MUHTE\u015eEM \u00dc\u00c7L\u00dc; SEN, BEN, M\u00dcZ\u0130K","source":"\u003ca href=\"http:\/\/www.twitter.com\" 
rel=\"nofollow\"\u003eTwitter for Windows\u003c\/a\u003e","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":2748960160,"id_str":"2748960160","name":"Enise Erkuzu\n",
"screen_name":"eniseerkuzu38","location":"Denizli\n","url":null,"description":"Tipe bakarak a\u015f\u0131k olanlar , am\u0131n\u0131za koyay\u0131m.",
"protected":false,"verified":false,"followers_count":36,"friends_count":32,"listed_count":0,"favourites_count":75,"statuses_count":595,"created_at":
"Thu Aug 21 10:17:18 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,
"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":
"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED",
"profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":
"http:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg",
"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,
"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"trends":[],"urls":[],"user_mentions":[{"screen_name":"metinsenturk","name":"Metin \u015eent\u00fcrk","id":523497734,"id_str":"523497734","indices":[24,37]}],
"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium","lang":"tr","timestamp_ms":"1410368494662"}

我的最终目标是从该文件中提取推文的文本,但为此,我需要将其作为json文件加载到python上,所以这就是我迄今为止所尝试的:

with open('tweets.json', 'r') as f:
    data = json.load(f) 

这是我收到的错误消息:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-42-212742fc8eeb> in <module>
      1 with open('tweets.json', 'r') as f:
----> 2     data = json.load(f)

/opt/anaconda3/lib/python3.7/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,
--> 296         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    297 
    298 

/opt/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/opt/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
    338         end = _w(s, end).end()
    339         if end != len(s):
--> 340             raise JSONDecodeError("Extra data", s, end)
    341         return obj
    342 

JSONDecodeError: Extra data: line 2 column 1 (char 153)

似乎有额外的数据,但我对处理json文件不太熟悉,我不知道到底是什么导致了错误以及如何修复它,你能给我指出正确的方向吗?到底是什么导致了这个错误?我怎样才能修复它?

共有2个答案

孟健
2023-03-14

在提供的json中,有多个json对象

{"delete":{"status":{"id":509743302972043264,"id_str":"509743302972043264","user_id":1366812392,"user_id_str":"1366812392"},"timestamp_ms":"1410368494532"}}

{"delete":{"status":{"id":64472572007428096,"id_str":"64472572007428096","user_id":31473446,"user_id_str":"31473446"},"timestamp_ms":"1410368494565"}}

{"created_at":"Wed Sep 10 17:01:34 +0000 2014","id":509748529070616576,"id_str":"509748529070616576","text":"Metin \u015eent\u00fcrk 
Twitterda @metinsenturk MUHTE\u015eEM \u00dc\u00c7L\u00dc; SEN, BEN, M\u00dcZ\u0130K","source":"\u003ca href=\"http:\/\/www.twitter.com\" 
rel=\"nofollow\"\u003eTwitter for Windows\u003c\/a\u003e","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":2748960160,"id_str":"2748960160","name":"Enise Erkuzu\n",
"screen_name":"eniseerkuzu38","location":"Denizli\n","url":null,"description":"Tipe bakarak a\u015f\u0131k olanlar , am\u0131n\u0131za koyay\u0131m.",
"protected":false,"verified":false,"followers_count":36,"friends_count":32,"listed_count":0,"favourites_count":75,"statuses_count":595,"created_at":
"Thu Aug 21 10:17:18 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,
"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":
"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED",
"profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":
"http:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg",
"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,
"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"trends":[],"urls":[],"user_mentions":[{"screen_name":"metinsenturk","name":"Metin \u015eent\u00fcrk","id":523497734,"id_str":"523497734","indices":[24,37]}],
"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium","lang":"tr","timestamp_ms":"1410368494662"}

因此,您必须将所有这些文件拆分为单独的文件或使文件如下

{
"all": [
{"delete":{"status":{"id":509743302972043264,"id_str":"509743302972043264","user_id":1366812392,"user_id_str":"1366812392"},"timestamp_ms":"1410368494532"}}

{"delete":{"status":{"id":64472572007428096,"id_str":"64472572007428096","user_id":31473446,"user_id_str":"31473446"},"timestamp_ms":"1410368494565"}}

{"created_at":"Wed Sep 10 17:01:34 +0000 2014","id":509748529070616576,"id_str":"509748529070616576","text":"Metin \u015eent\u00fcrk 
Twitterda @metinsenturk MUHTE\u015eEM \u00dc\u00c7L\u00dc; SEN, BEN, M\u00dcZ\u0130K","source":"\u003ca href=\"http:\/\/www.twitter.com\" 
rel=\"nofollow\"\u003eTwitter for Windows\u003c\/a\u003e","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":2748960160,"id_str":"2748960160","name":"Enise Erkuzu\n",
"screen_name":"eniseerkuzu38","location":"Denizli\n","url":null,"description":"Tipe bakarak a\u015f\u0131k olanlar , am\u0131n\u0131za koyay\u0131m.",
"protected":false,"verified":false,"followers_count":36,"friends_count":32,"listed_count":0,"favourites_count":75,"statuses_count":595,"created_at":
"Thu Aug 21 10:17:18 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,
"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":
"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED",
"profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":
"http:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/502399080686190592\/tRqoEQyM_normal.jpeg",
"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,
"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"trends":[],"urls":[],"user_mentions":[{"screen_name":"metinsenturk","name":"Metin \u015eent\u00fcrk","id":523497734,"id_str":"523497734","indices":[24,37]}],
"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium","lang":"tr","timestamp_ms":"1410368494662"}
]
}

并加载上述数据,如下所示:

with open("tweets.json", "r") as file:
    data = json.load(file)["all"]

现在,在上述情况下,您将获得存储在变量数据中的json文件中的所有内容的列表。

燕琨
2023-03-14

尝试使用类似https://jsonlint.com/.的东西验证JSON您会注意到文本不是有效的JSON,在第一个delete元素之后,您也遇到了JSON的多个问题。

 类似资料:
  • 我正试图使用pandas library的read\u json()和python的内置json库从python中的此链接加载数据集,该数据集存储在我的计算机中(与我的笔记本相同的文件夹)。以下是我的代码: 另一个是: 在第一种情况下,我得到以下错误: 在第二种情况下,我得到: 我正在Ubuntu 18.04中使用jupyter笔记本

  • 问题内容: 我有一个档案。它的简化版本如下所示: 我运行此脚本来解析它: 但我收到此错误: 您能指出我所缺少的吗? 问题答案: 您的JSON数据集无效,您可以将它们合并为一个对象数组。例如 : 在JSON中,您不能有多个顶级对象,但可以有对象数组,并且它是有效的 如果需要,可以在此链接中看到更多的JSON数据集示例。 如果您想了解有关JSON数组的更多信息,请阅读w3schools JSON教程

  • 我有问题从api接收数据与所有其他链接,这是可以的,但这一个是如此困难…这是代码 这是我正在使用的php代码。 在控制台上显示日志---->XMLHttpRequest无法加载文件:///d:/install/xampp/htdocs/allinone/proxy.php?url=https://na.api.pvp.n…pectatorgameinfo/NA1/26667724?api_key=

  • 因此,我将Json数据创建为question。Json Json: 和一个带有jQuery的调用函数: 但我会收到控制台错误,如: 无法加载文件:///c:/users/mirosz/desktop/project/test2/question.json:交叉源请求仅支持协议方案:http、data、chrome、chrome-extension、https 我做错了什么?!怎么加载那个文件?!

  • 我正在处理一个跨平台(html5)项目,稍后将在Android和iOS上启动,但我在使用从chrome浏览器本地的.json文件加载数据时遇到了一个问题。呼叫时 我得到了 null 我正在寻找一个本地加载json数据的解决方案。 非常感谢您的帮助!

  • 我有一个文件,其中包含以下数据: 我正在尝试插入一个名为“Date”的列作为第一列。 我使用的进出口商品如下: 但上述情况并没有如预期的那样起作用。它正在替换列数据。