Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从python中的嵌套数组中计算嵌套json对象_Python_Json - Fatal编程技术网

如何从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