【发布时间】:2020-03-20 14:53:36
【问题描述】:
这是来自页面源代码的一些 JSON:
match_info = JSON.parse('\x7B\x22id\x22\x3A\x2211768\x22,\x22fid\x22\x3A\x221376025\x22,\x22h\x22\x3A\x2272\x22,\x22a\x22\x3A\x2279\x22,\x22date\x22\x3A\x222019\x2D11\x2D23\x2015\x3A00\x3A00\x22,\x22league_id\x22\x3A\x221\x22,\x22season\x22\x3A\x222019\x22,\x22h_goals\x22\x3A\x220\x22,\x22a_goals\x22\x3A\x222\x22,\x22team_h\x22\x3A\x22Everton\x22,\x22team_a\x22\x3A\x22Norwich\x22,\x22h_xg\x22\x3A\x220.812693\x22,\x22a_xg\x22\x3A\x221.80849\x22,\x22h_w\x22\x3A\x220.1424\x22,\x22h_d\x22\x3A\x220.2108\x22,\x22h_l\x22\x3A\x220.6468\x22,\x22league\x22\x3A\x22EPL\x22,\x22h_shot\x22\x3A\x2218\x22,\x22a_shot\x22\x3A\x2213\x22,\x22h_shotOnTarget\x22\x3A\x227\x22,\x22a_shotOnTarget\x22\x3A\x225\x22,\x22h_deep\x22\x3A\x2215\x22,\x22a_deep\x22\x3A\x224\x22,\x22a_ppda\x22\x3A\x228.2174\x22,\x22h_ppda\x22\x3A\x225.9565\x22\x7D');
尝试使用json.loads() 方法将其转换为字典,但出现以下错误:
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
json.loads(shots)
File "C:\Users\turnc\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\turnc\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\turnc\AppData\Local\Programs\Python\Python37-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)
使用双引号也无济于事。
如何在 Python 中将此字符串转换为字典?
【问题讨论】:
标签: python json python-3.x string dictionary