[英]How to access different portions of a json data-structure?
{
"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"}
]
}
]
}
So, that I can access it as follows: 因此,我可以按以下方式访问它:
for cmd in file[commands][command]:
'name' = cmd['name']
shell = cmd['shell']
But, I am getting invalid syntax error. 但是,我收到了无效的语法错误。
You should use the json module to load the file correctly. 您应该使用json模块正确加载文件。
import json
command = json.load(open('file_name.json'))
name = command['commands']['command']['name']
you can read more about the module in the documents: https://docs.python.org/2/library/json.html 您可以在文档中阅读有关模块的更多信息: https : //docs.python.org/2/library/json.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.