代码之家  ›  专栏  ›  技术社区  ›  tomoc4

用python遍历json响应对象

  •  0
  • tomoc4  · 技术社区  · 6 年前

    我正在尝试遍历一个包含数据的json响应对象。我想循环json对象并提取 event-id , market-id ,和 event-participant-name

    [{
      "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']
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Selcuk    6 年前

    从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']