{
"commands" : [
{"command" : [
{"name" : "Base ls Command","shell" : "ls"},
{"name" : "Advanced ls command" : "shell" : "ls -la"}
]
},
{"command":[
{"name" :"Base netstat command", "shell" : "netstat"},
{"name" : "Advanced netstat command" : "shell" : "netstat -tunalp | grep LISTEN | grep nginx"}
]
}
]
}
所以,我可以按如下方式访问它:
for cmd in file[commands][command]:
'name' = cmd['name']
shell = cmd['shell']
但是,我的语法错误无效。
答案 0 :(得分:1)
您应该使用json模块正确加载文件。
import json
command = json.load(open('file_name.json'))
name = command['commands']['command']['name']
您可以在文档中阅读有关该模块的更多信息:https://docs.python.org/2/library/json.html