如何通过嵌套字典火柴书数据遍历JSON列表?

时间:2018-09-17 01:04:35

标签: python json

以下是我从json请求获取的部分数据。例如,许多比赛时间表中的一场比赛。

tennis_event_json = '''
    [
        {
            "TIMESTAMP": "2018-09-17 00:09:21.499540",
            "id": 910569150160041,
            "name": "A Rus vs V Flink",
            "sport-id": 9,
            "start": "2018-09-17T02:00:00.000Z",
            "in-running-flag": false,
            "allow-live-betting": true,
            "category-id": [
                9,
                410468520880009,
                456968853470009,
                476301248050010,
                595375589900009,
                899462538790042
            ],
            "status": "open",
            "volume": 1014.36488,
            "event-participants": [
                {
                    "id": 910569150220041,
                    "event-id": 910569150160041,
                    "participant-name": "V Flink",
                    "number": 2
                },
                {
                    "id": 910569150230042,
                    "event-id": 910569150160041,
                    "participant-name": "A Rus",
                    "number": 1
                }
            ],
            "markets": [
                {
                    "live": false,
                    "event-id": 910569150160041,
                    "id": 910569150980042,
                    "name": "Moneyline",
                    "runners": [
                        {
                            "withdrawn": false,
                            "prices": [],
                            "event-id": 910569150160041,
                            "id": 910569151040041,
                            "market-id": 910569150980042,
                            "name": "A Rus",
                            "status": "open",
                            "volume": 733.19472,
                            "event-participant-id": 910569150230042
                        },
                        {
                            "withdrawn": false,
                            "prices": [],
                            "event-id": 910569150160041,
                            "id": 910569151040042,
                            "market-id": 910569150980042,
                            "name": "V Flink",
                            "status": "open",
                            "volume": 281.17016,
                            "event-participant-id": 910569150220041
                        }
                    ],
                    "start": "2018-09-17T02:00:00.000Z",
                    "status": "open",
                    "market-type": "money_line",
                    "type": "binary",
                    "in-running-flag": false,
                    "allow-live-betting": true,
                    "volume": 1014.36488,
                    "back-overround": 141.27861,
                    "lay-overround": 58.72139,
                    "number-of-winners": 1
                }
            ],
            "meta-tags": [
                {
                    "id": 9,
                    "name": "Tennis",
                    "type": "SPORT",
                    "url-name": "tennis"
                },
                {
                    "id": 410468520880009,
                    "name": "Live Betting",
                    "type": "OTHER",
                    "url-name": "live-betting"
                },
                {
                    "id": 456968853470009,
                    "name": "Qualifiers",
                    "type": "OTHER",
                    "url-name": "qualifiers"
                },
                {
                    "id": 476301248050010,
                    "name": "Korea Rep",
                    "type": "COUNTRY",
                    "url-name": "korea-rep"
                },
                {
                    "id": 595375589900009,
                    "name": "WTA Seoul",
                    "type": "COMPETITION",
                    "url-name": "wta-seoul"
                },
                {
                    "id": 899462538790042,
                    "name": "September 16th 2018",
                    "type": "DATE",
                    "url-name": "september-16th-2018"
                }
            ],
            "Latency": 0.927759
        },
        {
            "TIMESTAMP": "2018-09-17 00:09:21.499540",
            "id": 910652994550042,
            "name": "C Liu vs D Khazaniuk",
            "sport-id": 9,
            "start": "2018-09-17T02:00:00.000Z",
            "in-running-flag": false,
            "allow-live-betting": true,
            "category-id": [
                9,
                410468520880009,
                423407498380010,
                456968853470009,
                595371288900009,
                899462538790042
            ],
            "status": "open",
            "volume": 328.95039,
            "event-participants": [
                {
                    "id": 910652994610041,
                    "event-id": 910652994550042,
                    "participant-name": "C Liu",
                    "number": 1
                },
                {
                    "id": 910652994610042,
                    "event-id": 910652994550042,
                    "participant-name": "D Khazaniuk",
                    "number": 2
                }
            ],
            "markets": [
                {
                    "live": false,
                    "event-id": 910652994550042,
                    "id": 910652994860042,
                    "name": "Moneyline",
                    "runners": [
                        {
                            "withdrawn": false,
                            "prices": [
                                {
                                    "available-amount": 274.14118,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.26315,
                                    "decimal-odds": 1.26315,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 364.54806,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.23255,
                                    "decimal-odds": 1.23255,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 289.15565,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.22727,
                                    "decimal-odds": 1.22727,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 112.42242,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.35715,
                                    "decimal-odds": 1.35715,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 277.83577,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.36765,
                                    "decimal-odds": 1.36765,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 150.76246,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 1.40651,
                                    "decimal-odds": 1.40651,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                }
                            ],
                            "event-id": 910652994550042,
                            "id": 910652994910041,
                            "market-id": 910652994860042,
                            "name": "C Liu",
                            "status": "open",
                            "volume": 253.72811,
                            "event-participant-id": 910652994610041
                        },
                        {
                            "withdrawn": false,
                            "prices": [
                                {
                                    "available-amount": 40.15086,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 3.8,
                                    "decimal-odds": 3.8,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 102.14551,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 3.72,
                                    "decimal-odds": 3.72,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 61.28556,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 3.46,
                                    "decimal-odds": 3.46,
                                    "side": "back",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 72.14242,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 4.8,
                                    "decimal-odds": 4.8,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 84.77862,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 5.3,
                                    "decimal-odds": 5.3,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                },
                                {
                                    "available-amount": 65.71719,
                                    "currency": "EUR",
                                    "odds-type": "DECIMAL",
                                    "odds": 5.4,
                                    "decimal-odds": 5.4,
                                    "side": "lay",
                                    "exchange-type": "back-lay"
                                }
                            ],
                            "event-id": 910652994550042,
                            "id": 910652994920042,
                            "market-id": 910652994860042,
                            "name": "D Khazaniuk",
                            "status": "open",
                            "volume": 75.22228,
                            "event-participant-id": 910652994610042
                        }
                    ],
                    "start": "2018-09-17T02:00:00.000Z",
                    "status": "open",
                    "market-type": "money_line",
                    "type": "binary",
                    "in-running-flag": false,
                    "allow-live-betting": true,
                    "volume": 328.95039,
                    "back-overround": 104.43889,
                    "lay-overround": 95.56111,
                    "number-of-winners": 1
                }
            ],
            "meta-tags": [
                {
                    "id": 9,
                    "name": "Tennis",
                    "type": "SPORT",
                    "url-name": "tennis"
                },
                {
                    "id": 410468520880009,
                    "name": "Live Betting",
                    "type": "OTHER",
                    "url-name": "live-betting"
                },
                {
                    "id": 423407498380010,
                    "name": "China",
                    "type": "COUNTRY",
                    "url-name": "china"
                },
                {
                    "id": 456968853470009,
                    "name": "Qualifiers",
                    "type": "OTHER",
                    "url-name": "qualifiers"
                },
                {
                    "id": 595371288900009,
                    "name": "WTA Guangzhou",
                    "type": "COMPETITION",
                    "url-name": "wta-guangzhou"
                },
                {
                    "id": 899462538790042,
                    "name": "September 16th 2018",
                    "type": "DATE",
                    "url-name": "september-16th-2018"
                }
            ],
            "Latency": 0.933854
        }
    ]
'''

可以通过以下方式将哪些版本加载到Python中以进行测试:

tennis_event = json.loads(tennis_event_json)

我想获取事件参与者的名称并将其存储为

runner_1 = event-participant[0]
runner_2 = event-participant[1]

,因为列表中的事件参与者有两个字典 我想对价格也这样做

runner_price_1 = odds[0]
runner_price_2 = odds[1]

这是我的代码:

for data in tennis_event:
    id = data['id']
    event_name = data['name']
    sport_id = data['sport-id']
    stat_time = data['start']
    is_ip = data['in-running-flag']

    for mar in data['event-participants']:
        runner_1 = mar['participant-name'][0]
        runner_2 = mar['participant-name'][1]
        for pr in data['prices']:
          runner_pr_1 = pr['odds'][0]
          runner_pr_2 = pr['odds'][1]
          print(runner_1, runner_2, runner_pr_1, runner_pr_2)

尝试代码时,我只会得到runner_pr_1的第一个字母。 我想做的是获取每个参与者的姓名,并为事件参与者dict中的第一个list命名为runner_pr_1,我想对第一个{{1 }}。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您应该执行以下操作。 但是,请注意,您可能必须匹配玩家ID,因为不能保证“跑步者”中的数据与参与者的顺序相同。

for data in tennis_event:
    id = data['id']
    event_name = data['name']
    sport_id = data['sport-id']
    stat_time = data['start']
    is_ip = data['in-running-flag']

    mar = data['event-participants']
    runner_1 = mar[0]['participant-name']
    runner_2 = mar[1]['participant-name']
    runner_pr_1 = data['markets'][0]['runners'][0]['prices'][0]['odds']
    runner_pr_2 = data['markets'][0]['runners'][1]['prices'][0]['odds']
    print(runner_1, runner_2, runner_pr_1, runner_pr_2)