gpt4 book ai didi

Python3 无法将字符串转换为 dict 或 json

转载 作者:行者123 更新时间:2023-12-01 09:19:15 31 4
gpt4 key购买 nike

r = requests.get(http_get_url, headers=headers)
r = r.text
r = r.replace("true", "True")
z = json.loads(r)

但是,我没有加载 json (或 python 字典),而是得到:

Traceback (most recent call last):
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 21, in <module>
get_errors(id)
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 17, in get_errors
z = json.loads(r)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 26 (char 28)

print(type(r))的结果是 <class 'str'>

以及 print(r) 的输出将“true”转换为“True”后为:

{
"HasItemsWithCount": True,
"Collection": [

{
"GroupId" : "14",
"Time" : "5/16/18, 5:02 PM",
"File" : "[ESCAPE[]]",
"Message" : "[ESCAPE[Client was restarted during backup, session may be incomplete.]]",
"Count" : "3"
},

]
}

所以我无法理解为什么格式正确的r字符串无法导入到json中。

此外,如果我获取此输出并通过复制/粘贴将其直接写入 python,则变量的类型为 dict,这表明文本的格式对于 Python 中的 dict 来说是正确的。所以我不确定为什么 json.loads 失败。

最佳答案

您的替换是向后的:

而不是:

r = r.replace("true", "True")

尝试:

r = r.replace("True", "true")

关于Python3 无法将字符串转换为 dict 或 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50939068/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com