如何从python中的嵌套数组中计算嵌套json对象
如何获得所有运动中的对象总数 目前,我正在咀嚼下面这段代码,但没有成功,尽管我所有的挖掘节点的尝试都失败了如何从python中的嵌套数组中计算嵌套json对象,python,json,Python,Json,如何获得所有运动中的对象总数 目前,我正在咀嚼下面这段代码,但没有成功,尽管我所有的挖掘节点的尝试都失败了 import json RESULTS = 'sample.json' with open(RESULTS) as f: data = json.load(f) results = data print(len(results[0]['result']['movements'])) #Expected: 3 sample.json [ { “结果”:{ “TempId”
import json
RESULTS = 'sample.json'
with open(RESULTS) as f:
data = json.load(f)
results = data
print(len(results[0]['result']['movements'])) #Expected: 3
sample.json
[
{
“结果”:{
“TempId”:“369477387”,
“运动”:[
{
“日期”:“2018-05-03”,
“信用”:100.0,
“短文本”:“我的
费用”
},
{
“日期”:“2018-05-03”,
“借方”:200.0,
“短文本”:“我的
F\u00dcR”
}
]
}
},
{
“结果”:{
“TempId”:“369477395”,
“运动”:[]
}
},
{
“结果”:{
“TempId”:“369477402”,
“运动”:[
{
“日期”:“2018-05-07”,
“信用”:100.0,
“短文本”:“我的
费用”
}
]
}
}
]
使用列表理解,您可以从样本列表中的每个元素中找出移动的数量
然后只需在count
列表上应用sum
方法,即可找到总和
count = sum([len(item['result']['movements']) for item in sample])
输出
>> count
3
>> count
3