my_json文件有嵌套字典列表,如果type==Developer-Verified and it's value==1
,我只需要打印用户名,我设法只打印批准列表,无法进一步。
$ cat myjson_file | python3.6 -c "import sys, json; approvals=json.load(sys.stdin)['currentPatchSet']['approvals']; print(json.dumps(approvals, indent=4))"
[
{
"type": "Developer-Verified",
"description": "Developer-Verified",
"value": "1",
"grantedOn": 1581451370,
"by": {
"name": "Donald Snifer",
"email": "dsnifer@gmail.com",
"username": "dsnifer"
}
},
{
"type": "Code-Review",
"description": "Code-Review",
"value": "2",
"grantedOn": 1581623684,
"by": {
"name": "Brandon Welch",
"email": "bwelch@gmail.com",
"username": "bwelch"
}
},
{
"type": "Developer-Verified",
"description": "Developer-Verified",
"value": "1",
"grantedOn": 1581451370,
"by": {
"name": "Hamlin Damer",
"email": "hdamer@gmail.com",
"username": "hdamer"
}
}
]
$
我只需要打印dsnifer hdamer
我试图进一步与以下和其他逻辑,我一直失败python3.6 -c "import sys, json; approvals=json.load(sys.stdin)['currentPatchSet']['approvals']; print( k for k,v in approvals[0].items())"
在json内部循环并检查type和value是否满足条件如果满足则打印用户名
for ele in jsonData :
if ele['type'] == 'Developer-Verified' and ele['value'] == '1' :
print(ele["by"]["username"], end=" ")