我尝试遍历json对象以检索名为matched-bets
的列表中的数据,而我得到KeyError: 'matched-bets'
如何从嵌套列表中检索属性?
data =[
{
'TIMESTAMP': '2019-01-20 04:56:54.609618',
'id': 1019913470680020,
'event-id': 1018510669860015,
'event-name': 'Tomas Berdych vs Rafael Nadal',
'market-id': 1018510670260016,
'market-name': 'Moneyline',
'market-type': 'money_line',
'runner-id': 1018510670340016,
'runner-name': 'Rafael Nadal',
'sport-id': 9,
'temp-id': '2.1547948212610',
'exchange-type': 'back-lay',
'side': 'back',
'odds': 3.0,
'odds-type': 'DECIMAL',
'decimal-odds': 3.0,
'stake': 3,
'remaining': 3,
'potential-profit': 6.0,
'remaining-potential-profit': 6.0,
'commission-type': 'NET_WIN',
'originator-commission-rate': 0.02,
'acceptor-commission-rate': 0.02,
'commission-reserve': 0,
'currency': 'EUR',
'created-at': '2019-01-20T01:36:58.498Z',
'status': 'flushed',
'in-play': False,
'Latency': 0.074342
},
{
'TIMESTAMP': '2019-01-20 04:56:54.609618',
'id': 1019938516780022,
'event-id': 1018426645920015,
'event-name': 'Frances Tiafoe vs Grigor Dimitrov',
'market-id': 1018426646320016,
'market-name': 'Moneyline',
'market-type': 'money_line',
'runner-id': 1018426646390015,
'runner-name': 'Frances Tiafoe',
'sport-id': 9,
'temp-id': '3.1547950690507',
'exchange-type': 'back-lay',
'side': 'back',
'odds': 3.5,
'odds-type': 'DECIMAL',
'decimal-odds': 3.5,
'stake': 200,
'remaining': 1e-05,
'potential-profit': 500.0,
'remaining-potential-profit': 3e-05,
'commission-type': 'NET_WIN',
'originator-commission-rate': 0.02,
'acceptor-commission-rate': 0.02,
'commission-reserve': 0,
'currency': 'EUR',
'created-at': '2019-01-20T02:18:43.108Z',
'status': 'matched',
'in-play': True,
'matched-bets': [
{
'id': 1019941163310322,
'offer-id': 1019938516780022,
'odds': 3.5,
'odds-type': 'DECIMAL',
'decimal-odds': 3.5,
'stake': 102.84399,
'potential-profit': 257.10998,
'commission': 2.0568798,
'currency': 'EUR',
'status': 'open',
'created-at': '2019-01-20T02:23:07.761Z'
},
{
'id': 1019941305850222,
'offer-id': 1019938516780022,
'odds': 3.5,
'odds-type': 'DECIMAL',
'decimal-odds': 3.5,
'stake': 97.156,
'potential-profit': 242.89,
'commission': 1.94312,
'currency': 'EUR',
'status': 'open',
'created-at': '2019-01-20T02:23:22.015Z'
}
],
'Latency': 0.080931
},
{
'TIMESTAMP': '2019-01-20 04:56:54.609618',
'id': 1020003691980022,
'event-id': 1018426645920015,
'event-name': 'Frances Tiafoe vs Grigor Dimitrov',
'market-id': 1018426646320016,
'market-name': 'Moneyline',
'market-type': 'money_line',
'runner-id': 1018426646390015,
'runner-name': 'Frances Tiafoe',
'sport-id': 9,
'temp-id': '1.1547951656615',
'exchange-type': 'back-lay',
'side': 'lay',
'odds': 1.25641,
'odds-type': 'DECIMAL',
'decimal-odds': 1.25642,
'stake': 200,
'remaining': 0.0,
'potential-liability': 51.28205,
'remaining-potential-liability': 0.0,
'commission-type': 'NET_WIN',
'originator-commission-rate': 0.02,
'acceptor-commission-rate': 0.02,
'commission-reserve': 0,
'currency': 'EUR',
'created-at': '2019-01-20T04:07:20.628Z',
'status': 'matched',
'in-play': True,
'matched-bets': [
{
'id': 1020008680510122,
'offer-id': 1020003691980022,
'odds': 1.25641,
'odds-type': 'DECIMAL',
'decimal-odds': 1.25642,
'stake': 200.0,
'potential-liability': 51.28205,
'commission': 1.025641,
'currency': 'EUR',
'status': 'open',
'created-at': '2019-01-20T04:15:39.481Z'
}
],
'Latency': 0.08113
},
{
'TIMESTAMP': '2019-01-20 04:56:54.609618',
'id': 1020020358220022,
'event-id': 1018559986530015,
'event-name': 'Danielle Rose Collins vs Angelique Kerber',
'market-id': 1018559986940016,
'market-name': 'Moneyline',
'market-type': 'money_line',
'runner-id': 1018559987020016,
'runner-name': 'Angelique Kerber',
'sport-id': 9,
'temp-id': '5.1547958894686',
'exchange-type': 'back-lay',
'side': 'back',
'odds': 2.36,
'odds-type': 'DECIMAL',
'decimal-odds': 2.36,
'stake': 200,
'remaining': 1e-05,
'potential-profit': 272.0,
'remaining-potential-profit': 1e-05,
'commission-type': 'NET_WIN',
'originator-commission-rate': 0.02,
'acceptor-commission-rate': 0.02,
'commission-reserve': 0,
'currency': 'EUR',
'created-at': '2019-01-20T04:35:07.252Z',
'status': 'matched',
'in-play': True,
'matched-bets': [
{
'id': 1020020481880222,
'offer-id': 1020020358220022,
'odds': 2.36,
'odds-type': 'DECIMAL',
'decimal-odds': 2.36,
'stake': 199.99999,
'potential-profit': 271.99999,
'commission': 3.9999998,
'currency': 'EUR',
'status': 'open',
'created-at': '2019-01-20T04:35:19.618Z'
}
],
'Latency': 0.08125
}
]
for d in data:
id = d['matched-bets'][0]['id']
print(id)
答案 0 :(得分:0)
I tried your json object. I change "in-play": false,
到"in-play": False,
,它工作正常。
尝试将false
更改为False
更新:
import json
# its load your api data
json_data = json.loads(data)
# Convert to python object
python_object_data=json.dumps(json_data)
#
for d in python_object_data:
id = d['matched-bets'][0]['id']
print(id)
如果您使用更新的数据格式,请使用:
for d in data:
print(d['id'])
答案 1 :(得分:0)
您可以像下面这样写
for _d in data:
if hasattr(_d, 'matched-bets'):
print(_d['matched-bets']['id'])