Python json:json.decoder.JSONDecodeError:预期值:Python应用程序中的第1行第1列(字符0)

2024-04-16 11:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我四处寻找,但是没有一个解决方案有效。我有一个WSGI应用程序正在Python上使用Flask。我正在尝试解析一个json文件,该文件也是在另一个应用程序路径中使用Python中的json库创建的。下面是我如何读取并尝试解析该文件

 with open("data/accounts/names/" + username + ".json", "r+") as read_file:
            uid = json.load(read_file)

这是文件的内容

{"username": "username", "name": "Name", "uid": "227072665949", "following": [""], "followers": [""], "posts": [""], "joined": [""], "device": "835527195310", "liked": [""]}

这是完整的错误消息

ERROR in app: Exception on /api/loginuser [GET]
Traceback (most recent call last):
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\##\Desktop\stella\xick\app.py", line 62, in logat
    uid = json.load(read_file)
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\##\AppData\Local\Programs\Python\Python38-32\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)

此外,我注意到每当我试图运行代码时,代码总是被抹去,我也尝试过:

uidfile = open("data/accounts/names/" + username + ".json", "r")
uid = json.loads(uidfile.read())

但得到同样的回答。我还尝试使用linter验证我的json,似乎没有出现任何问题。该文件是用utf-8编码的,当我尝试对其内容执行print()时,该文件似乎没有问题


Tags: 文件inpyjsonapplibpackageslocal
1条回答
网友
1楼 · 发布于 2024-04-16 11:20:22

文件开头很可能有一个空字节

下面是在前面插入空字节的更简单测试文件的情况:

$ od -ba test.json
0000000 000 173 042 165 163 145 162 156 141 155 145 042 072 040 042 165
        nul   {   "   u   s   e   r   n   a   m   e   "   :  sp   "   u
0000020 163 145 162 156 141 155 145 042 175 012
          s   e   r   n   a   m   e   "   }  nl

$ python3
>>> import json
>>> with open('test.json', 'r') as f:
...     j = json.load(f)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.4/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
>>> 

删除前导null,您应该会没事的

相关问题 更多 >