首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python只与Json中在特定对象值之后的对象进行交互?

如何使用Python只与Json中在特定对象值之后的对象进行交互?
EN

Stack Overflow用户
提问于 2022-03-17 14:24:05
回答 1查看 19关注 0票数 0

我正在收集按顺序添加到JSON中的数据,但是由于它们都是相加在一起的,所以它们没有很好的分离,所以我需要克服这个困难。

我的JSON遵循以下模式:

代码语言:javascript
复制
{
    "content": {
      "matchFacts": {
        "events": {
          "events": [
            {
              "type": "Substitution",
              "time": 33
            },
            {
              "type": "Card",
              "time": 34
            },
            {
              "type": "Goal",
              "time": 38            
            },
            {
              "type": "Card",
              "time": 43            
            },
            {
              "type": "AddedTime",
              "time": 45
            },
            {
              "type": "Goal",
              "time": 45
            },
            {
              "type": "Half",
              "time": 45
            },
            {
              "type": "Substitution",
              "time": 46
            },
            {
              "type": "Substitution",
              "time": 58
            },
            {
              "type": "Substitution",
              "time": 58
            },
            {
              "type": "Goal",
              "time": 71
            },
            {
              "type": "Substitution",
              "time": 74
            },
            {
              "type": "Substitution",
              "time": 74
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 83
            },
            {
              "type": "AddedTime",
              "time": 90
            },
            {
              "type": "Card",
              "time": 90
            },
            {
              "type": "Half",
              "time": 90
            }
          ]
      }
    }
  }
}

假设我只需要收集第一个"type": "Goal"出现之前的"type": "Half",所以我这样做:

代码语言:javascript
复制
search_goals = response_json['content']['matchFacts']['events']['events']
goals_ht = 0
for event in search_goals:
    if (event['type'] == 'Half'):
        break
    elif (event['type'] == 'Goal'):
        goals_ht += 1

但是,当我只需要收集第一个"type": "Goal"之后的"type": "Half"时,我应该如何继续呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-17 14:29:38

根据您的结构,一个可能的解决方案是创建一个False标志,一旦到达Half事件,就会切换到True,然后在对Goal事件执行某些操作时,只需检查该标志:

代码语言:javascript
复制
search_goals = response_json['content']['matchFacts']['events']['events']

reached_ht = False
for event in search_goals:
    if event['type'] == 'Half':
        reached_ht = True
    elif event['type'] == 'Goal' and reached_ht:
        # ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71513991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档