首页 > 解决方案 > json 导入是否因为 JSON 文件而失败?

问题描述

我有一个简单的 python 程序,它应该将 .json 文件加载到变量中并将变量输出打印到屏幕上。但它无法运行并出现以下消息错误:

"File "/Documents/VSCode/python-3-programming-specialization/Data Collection and Processing with Python (Course 3)/Lesson 1/assessment6.py", line 5, in <module>
    res = json.load(json_file,)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 15 column 25 (char 506)"

在 JSON 文件中也有一个错误,我认为是问题所在,但我不知道要设置什么值来清除 JSON 文件中的错误。错误发生在您看到 None、True 或 False 值的任何地方。

JSON 错误消息:“值预期 json(516)

    "statuses": [
      {
        "contributors": None,
        "truncated": False,
        "text": "RT @mikeweber25: I'm decommiting from the university of Michigan thank you Michigan for the love and support I'll remake my decision at the\u2026",
        "in_reply_to_status_id": None,
        "id": 536624519285583872,
        "favorite_count": 0,
        "source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
        "retweeted": False,
        "coordinates": None,
import json

with open('./tweets.json') as json_file:
    res = json.load(json_file)

print(res)

预期的输出应该是 python 程序读入的文件。

标签: jsonpython-3.xvisual-studio-code

解决方案


推荐阅读