使用嵌套的字典和列表解析 JSON

Posted

技术标签:

【中文标题】使用嵌套的字典和列表解析 JSON【英文标题】:Parsing JSON with nested Dict & Lists 【发布时间】:2021-09-25 17:11:03 【问题描述】:

我对 Python 很陌生,并且可以有效地使用库。我目前无法将 JSON 解析为 Dataframe。

这是我的字典:

   data =  
 "event": 
  "id": 323725,
  "code": 981,
  "sport": "Football",
  "tournament": "Bresil D1",
  "name": "Sao Paulo - Fortaleza Ce",
  "homeTeam": "Sao Paulo",
  "awayTeam": "Fortaleza Ce",
  "markets": [
   
    "marketName": "R\u00e9sultat Final",
    "status": "active",
    "antepost": false,
    "marketID": "0",
    "marketFixedID": "285",
    "columnType": 0,
    "marketGroupID": [
     "g_s-441_11111111",
     "g_s-441_261",
     "g_s-441_11111111",
     "g_s-441_11111111"
    ],
    "marketOrder": 1,
    "odds": [
     
      "id": "323725_0_400",
      "name": "SAO PAULO",
      "short": "SAO PAULO",
      "clean": "1",
      "status": "active",
      "odd": 1.85,
      "handicap": 0
     ,
     
      "id": "323725_0_401",
      "name": "X",
      "short": "X",
      "clean": "X",
      "status": "active",
      "odd": 2.65,
      "handicap": 0
     ,
     
      "id": "323725_0_402",
      "name": "FORTALEZA CE",
      "short": "FORTALEZA CE",
      "clean": "2",
      "status": "active",
      "odd": 3.5,
      "handicap": 0
     
    ],
    "minimumRestriction": 1
   ,
   
    "marketName": "R\u00e9sultat Final avec Handicap (0:1)",
    "status": "active",
    "antepost": false,
    "marketID": "10",
    "marketFixedID": "295",
    "columnType": 0,
    "marketGroupID": [
     "g_s-441_11111111",
     "g_s-441_265",
     "g_s-441_11111111",
     "g_s-441_11111111"
    ],
    "marketOrder": 2,
    "isHandicap": true,
    "var0": -1,
    "odds": [
     
      "id": "323725_10_445",
      "name": "1",
      "short": "1",
      "clean": "1",
      "status": "active",
      "odd": 2.6,
      "handicap": 0
     ,
     
      "id": "323725_10_446",
      "name": "X",
      "short": "X",
      "clean": "X",
      "status": "active",
      "odd": 2.9,
      "handicap": 0
     ,
     
      "id": "323725_10_447",
      "name": "2",
      "short": "2",
      "clean": "2",
      "status": "active",
      "odd": 1.75,
      "handicap": 0
     
    ],
    "minimumRestriction": 1
   ,
   
    "marketName": "Moins\/Plus 1,5",
    "status": "active",
    "antepost": false,
    "marketID": "19",
    "marketFixedID": "304",
    "columnType": 2,
    "marketGroupID": [
     "g_s-441_11111111",
     "g_s-441_11111111",
     "g_s-441_11111111",
     "g_s-441_11111111"
    ],
    "marketOrder": 4,
    "isHandicap": false,
    "var0": 1.5,
    "odds": [
     
      "id": "323725_19_469",
      "name": "Moins ",
      "short": "Moins ",
      "clean": "Moins ",
      "status": "active",
      "odd": 2.65,
      "handicap": 0
     ,
     
      "id": "323725_19_470",
      "name": "Plus",
      "short": "Plus",
      "clean": "Plus",
      "status": "active",
      "odd": 1.15,
      "handicap": 0
     
    ],
    "minimumRestriction": 1
   ,

我的目标是将这个 dict 解析成一个 Dataframe,其中包含“marketName”和每个单独的“odd”以及它的“name”。

首先我尝试像这样提取想要的数据:

markets = data['event']['markets']
for m in markets:

 marketName= m['marketName']
 odds = m['odds']

从这里我不知道如何处理这些数据并将其正确放入一个相关的数据帧中

【问题讨论】:

请发布您预期的输出数据框 您好!因此,第一个答案中显示的数据框是完美的。但它只返回最后的赔率列表。谢谢 【参考方案1】:

尝试使用字典理解和pd.DataFrame.join

markets = pd.DataFrame(k: v for i in data['event']['markets'] for k, v in i.items() if k in ['marketName', 'odds'])
markets = markets[['marketName']].join(pd.DataFrame(markets['odds'].tolist()).add_prefix('odds_'))

现在:

print(markets)

输出:

        marketName odds_clean  odds_handicap        odds_id odds_name  \
0  Moins\/Plus 1,5     Moins               0  323725_19_469    Moins    
1  Moins\/Plus 1,5       Plus              0  323725_19_470      Plus   

   odds_odd odds_short odds_status  
0      2.65     Moins       active  
1      1.15       Plus      active  

【讨论】:

感谢您的回答。这是我正在寻找的那种 DataFrame。你知道为什么它只返回 JSON 中的最后一个赔率吗?

以上是关于使用嵌套的字典和列表解析 JSON的主要内容,如果未能解决你的问题,请参考以下文章

flutter : 嵌套的 json 解析列表

如何使用从 Flutter 中的 json 解析的嵌套映射列表中的函数创建对象

Spark:如何解析嵌套列表的 JSON 字符串以触发数据框?

在flutter中解析嵌套的JSON列表时出错

在 IOS 中解析嵌套的 Json

使用 Newtonsoft 解析带有嵌套和变量字典的 Json [重复]