【问题标题】:raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)提高 JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
【发布时间】:2021-08-25 07:40:25
【问题描述】:

我想创建一个记录,我部分成功了。但是这里的问题,我不能记录2,我得到以下错误。我做错了什么?

错误:

  File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

示例代码:

def loadUsers(self):
        # Dosya var?
        if os.path.exists('AccInformation.json'): # True ise......
            with open('AccInformation.json', 'r', encoding='utf-8') as file:
                users = json.load(file)
                
                for user in users:
                    user = json.load(user)
                    newUser = Account(user_id = user['user_id'], firstName = user['first_name'], lastName = user['last_name'], 
                                      email = user['email'], username = user['username'], 
                                      password = user['password'], accountKEY = user['AccountKEY'])
                    
                    self.users.append(newUser)
                print(self.users)
        else:
            print("""'AccInformation' adlı Dosya bulunamadı.""")

【问题讨论】:

  • AccInformation.json的内容是什么?
  • 您确定 AccInformation.json 包含有效的 JSON 吗?此外, user = json.load(user) 将不起作用,因为 user 将是一个字符串而不是文件描述符
  • AccInformation.json 内容此; ["{\"user_id\": 518990642826, \"firstName\": \"Asdf\", \"lastName\": \"CCCC\", \"email\": \"eee@gmail.com\" , \"用户名\": \"Einsatzgruppens\", \"密码\": \"123456\", \"accountKEY\": \"31AI-TR9F-6GMP-S7DE-KJOC-V0Z4\"}"]

标签: python json


【解决方案1】:

因此,如果不知道 AccInformation.json 的内容,很难正确回答这个问题,但我想它是一个 JSON,其中包含代表不同用户的 dict 项目列表。

基于此,下面的代码应该可以工作...

import os
import json


def loadUsers(self):
    # Dosya var?
    if os.path.exists("AccInformation.json"):  # True ise......
        with open("AccInformation.json", encoding="utf-8") as infile:
            users = json.load(infile)
        # return indent here as we've already loaded the file

        for user in users:
            newUser = Account(
                user_id=user["user_id"],
                firstName=user["first_name"],
                lastName=user["last_name"],
                email=user["email"],
                username=user["username"],
                password=user["password"],
                accountKEY=user["AccountKEY"],
            )

            self.users.append(newUser)
        print(self.users)
    else:
        print("""'AccInformation' adlı Dosya bulunamadı.""")

【讨论】:

  • json文件内容如下; ["{\"user_id\": 518990642826, \"ad\": \"Asdf\", \"soyad\": \"CCCC\", \"email\": \"eee@gmail.com\" , \"kullanıcı adı\": \"Einsatzgruppens\", \"parola\": \"123456\", \"accountKEY\": \"31AI-TR9F-6GMP-S7DE-KJOC-V0Z4\"}"]我已经更正了我的代码,它与您现在正在编写的代码相同。我收到以下错误。我的目标是创建新用户并将它们保存到 json 文件中。 TypeError:字符串索引必须是整数
  • 所以问题出在 JSON 本身。简单地说,它是一个以单个字符串为内容的列表。第一个 " 和最后一个 " 是唯一的非转义引号。如果你在 JSON 中的引号前加上一个 \ ,你就会转义它。进行查找和替换,将\" 替换为" ,并删除第一个和最后一个。然后它就变成了你需要的 JSON...
【解决方案2】:

我刚刚解决了这个问题。这正是问题所在:“user = json.load(user)”

Ben bunu şöyle düzelttim : "user = json.loads(user)"。

他现在工作没有问题。希望他以后不要给我添麻烦:)

感谢大家的宝贵时间。

【讨论】:

    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2022-06-11
    • 2018-11-12
    • 2020-04-19
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多