【发布时间】:2019-06-30 14:20:50
【问题描述】:
我有一个 curl 命令,它返回一些 json 结果。
{
"all":[
{
"id":"1",
"actions":[
"power",
"reboot"
]
},
{
"id":"2",
"actions":[
"shutdown"
]
},
{
"id":"3",
"actions":[
"backup"
]
}
]
}
我使用此命令检索数据操作:
curl -s https://DOMAIN/API -H "X-Auth-Token: TOKEN" | python -c "import sys, json, re; print [ i['allowed_actions'] for i in json.load(sys.stdin)['servers']]"
但我想在命令行的 python 中使用这段代码:
for i in json.load(sys.stdin)['all']:
if i['id'] == '1':
print(i['actions'])
我试过了:
curl -s https://DOMAIN/API -H "X-Auth-Token: TOKEN" | python -c "import sys, json, re; print [ if i['id'] == '1': i['actions'] for i in json.load(sys.stdin)['servers']]"
但它返回一个语法错误
File "<string>", line 1
import sys, json, re; for i in json.load(sys.stdin)['all']:\nif i['id'] == '1':\nprint(i['actions'])
^
SyntaxError: invalid syntax
【问题讨论】:
标签: python json python-2.7 shell