【问题标题】:How to decode json from page source code to a dictionary?如何将页面源代码中的 json 解码为字典?
【发布时间】: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


    【解决方案1】:

    试试这个 - 将其转换为带有 b string prefixbytes 类型对象:

    >>> json.loads(b'\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')
    {'id': '11768',
     'fid': '1376025',
     'h': '72',
     'a': '79',
     'date': '2019-11-23 15:00:00',
     'league_id': '1',
     'season': '2019',
     'h_goals': '0',
     'a_goals': '2',
     'team_h': 'Everton',
     'team_a': 'Norwich',
     'h_xg': '0.812693',
     'a_xg': '1.80849',
     'h_w': '0.1424',
     'h_d': '0.2108',
     'h_l': '0.6468',
     'league': 'EPL',
     'h_shot': '18',
     'a_shot': '13',
     'h_shotOnTarget': '7',
     'a_shotOnTarget': '5',
     'h_deep': '15',
     'a_deep': '4',
     'a_ppda': '8.2174',
     'h_ppda': '5.9565'}
    

    或者用results变量中的字符串:

    >>> json.loads(bytes(result, 'utf-8'))
    {'id': '11768',
     'fid': '1376025',
     'h': '72',
     'a': '79',
     'date': '2019-11-23 15:00:00',
     'league_id': '1',
     'season': '2019',
     'h_goals': '0',
     'a_goals': '2',
     'team_h': 'Everton',
     'team_a': 'Norwich',
     'h_xg': '0.812693',
     'a_xg': '1.80849',
     'h_w': '0.1424',
     'h_d': '0.2108',
     'h_l': '0.6468',
     'league': 'EPL',
     'h_shot': '18',
     'a_shot': '13',
     'h_shotOnTarget': '7',
     'a_shotOnTarget': '5',
     'h_deep': '15',
     'a_deep': '4',
     'a_ppda': '8.2174',
     'h_ppda': '5.9565'}
    

    【讨论】:

    • 它确实有效,但我怎样才能将 json 作为变量而不是字符串文字进行相同的传递?
    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多