我正在收集按顺序添加到JSON中的数据,但是由于它们都是相加在一起的,所以它们没有很好的分离,所以我需要克服这个困难。
我的JSON遵循以下模式:
{
"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",所以我这样做:
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"时,我应该如何继续呢?
发布于 2022-03-17 14:29:38
根据您的结构,一个可能的解决方案是创建一个False标志,一旦到达Half事件,就会切换到True,然后在对Goal事件执行某些操作时,只需检查该标志:
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:
# ...https://stackoverflow.com/questions/71513991
复制相似问题