我正在尝试遍历包含数据的json响应对象。我想循环以为json对象并提取
键event-id
,market-id
和event-participant-name
。 json响应如下
[{
"TIMESTAMP": "2018-09-05 22: 59: 44.398534 ",
"id": 900652866170042,
"name": "C Suarez Navarro vs M Keys",
"sport-id": 9,
"start": "2018-09-05T23:10:00.000Z",
"in-running-flag": false,
"allow-live-betting": true,
"category-id": [
9,
399952692940010,
410468520880009,
573630974180009,
613128376040013,
643136938410012,
894084819950041
],
"status": "open",
"volume": 83821.22796,
"event-participants": [{
"id": 900652866280041,
"event-id": 900652866170042,
"participant-name": "C Suarez Navarro",
"number": 1
},
{
"id": 900652866290042,
"event-id": 900652866170042,
"participant-name": "M Keys",
"number": 2
}
],
"markets": [{
"live": false,
"event-id": 900652866170042,
"id": 900652866490041,
"name": "Moneyline",
"runners": [{
"withdrawn": false,
"prices": [{
"available-amount": 1390.32516,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.44,
"decimal-odds": 3.44,
"side": "back",
"exchange-type": "back-lay"
},
{
"available-amount": 12.22,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.36,
"decimal-odds": 3.36,
"side": "back",
"exchange-type": "back-lay"
},
{
"available-amount": 38.84366,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.34,
"decimal-odds": 3.34,
"side": "back",
"exchange-type": "back-lay"
},
{
"available-amount": 1843.65097,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.48,
"decimal-odds": 3.48,
"side": "lay",
"exchange-type": "back-lay"
},
{
"available-amount": 27.82505,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.5,
"decimal-odds": 3.5,
"side": "lay",
"exchange-type": "back-lay"
},
{
"available-amount": 11.20312,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 3.56,
"decimal-odds": 3.56,
"side": "lay",
"exchange-type": "back-lay"
}
],
"event-id": 900652866170042,
"id": 900652866590042,
"market-id": 900652866490041,
"name": "C Suarez Navarro",
"status": "open",
"volume": 25342.31304,
"event-participant-id": 900652866280041
},
{
"withdrawn": false,
"prices": [{
"available-amount": 4572.25441,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 1.40322,
"decimal-odds": 1.40322,
"side": "back",
"exchange-type": "back-lay"
},
{
"available-amount": 69.56263,
"currency": "EUR",
"odds-type": "DECIMAL",
"odds": 1.4,
"decimal-odds": 1.4,
"side": "back",
"exchange-type": "back-lay"
},
当我循环以为这个JSON对象时,我得到一个关键错误。如何遍历此对象并为每个属性返回每个值?
代码
def match_book_get(self):
tennis_events = self.api.market_data.get_events(sport_ids=
[9],states=MarketStates.All, per_page=10000, offset=0,
include_event_participants=Boolean.T,
price_depth=3, side=Side.All)
data = []
for data in tennis_events:
event_id = data['event_id']
market_id = data['market-id']
evparid = data['event-participant-id']
答案 0 :(得分:1)
从JSON响应中可以看出,您要查找的键位于markets
和runners
键下的列表中。您应该使用类似以下的内容:
for data in tennis_events:
for market in data['markets']:
event_id = market['event-id']
for runner in market['runners']:
market_id = runner['market-id']
evparid = runner['event-participant-id']