从None JSONDecodeError(“期待值”,s,err.value)中提取json.decoder.JSONDecodeError: Expecting值:第1列(char 0)
是在运行以下代码时遇到的错误:
read = open('sample.json')
@app.get("/key/{hole}", status_code=200)
def fetch_message(*, hole: int):
data = json.load(read)
for i in data:
if i['id'] == hole:
return(i['message'])
break我的json文件看起来如下所示:
{
"id": 0,
"name": "John Doe",
"message": "Hello World!"
}发布于 2022-09-30 17:34:17
您正在尝试迭代json数据中单个条目的键。我相信您想要的是迭代json数据的条目列表,所以您的sample.json应该是这样的:
[
{
"id": 0,
"name": "John Doe",
"message": "Hello World!"
}
]发布于 2022-09-30 21:19:14
您正在尝试在json上迭代,显然这不太好。
此版本适用于包含1个json对象的文件,就像您的文件一样。
read = open('sample.json')
@app.get("/key/{hole}", status_code=200)
def fetch_message(*, hole: int):
data = json.load(read)
if data['id'] == hole:
return(data['message'])
break # this is not reacheablehttps://stackoverflow.com/questions/73911846
复制相似问题