【问题标题】:why am i getting this error when reading a .json file?为什么读取 .json 文件时出现此错误?
【发布时间】:2019-12-11 20:03:30
【问题描述】:

所以我有这个函数来读取一个 json 文件并返回通过它传递的文件中的数据字典

def file_read(source):
    with open(source) as file:
        data = file.read()
        dictionary = json.loads(data)
        return dictionary

我使用的文件名为“users.json”并已格式化

{"Jim":("password", "MMA60V")}

但是,每当我尝试运行代码以读取“users.json”中的内容时,我都会收到此错误

json.decoder.JSONDecodeError: Expecting value: line 1 column 8 (char 7)```

does anyone know why this is happening and a possible solution thank you.

【问题讨论】:

  • 试试:{"Jim":["password", "MMA60V"]} JSON 格式使用方括号[ ] 表示数组。

标签: python json file-handling


【解决方案1】:

这不是正确的 JSON,因为您使用的是 json.loads,所以您会收到错误。

将 JSON 粘贴到 https://jsonlint.com/ 中,您可以看到输入无效。 JSON 不支持元组。

【讨论】:

  • 成功了,谢谢,我会尽可能将其标记为答案
猜你喜欢
  • 2012-05-02
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2021-10-29
  • 2017-01-30
  • 2020-10-11
相关资源
最近更新 更多