【发布时间】:2019-11-19 03:52:46
【问题描述】:
我正在尝试遍历 JSON 配置文件,并一次处理每个服务。我想在这段代码中获取 service_name,但它给了我一个 KeyError: 0。
我尝试过以各种方式循环它,但我尝试的每种方法都给了我关键错误。
{
"my_services": [
{
"service_name" : "Exchange Online",
"region": [
"NorthCentral",
"SouthCentral"
],
"firewall": [
"ABC",
"DEF"
],
"firewall_ip" : "12.23.34.455",
"firewall_type" : "cde",
"endpointURL" : "something.com",
"parserType" : "parseO365Delta.py"
},
{
"service_name" : "Microsoft 365 Common and Office Online",
"region": [
"NorthCentral",
"SouthCentral"
],
"firewall": [
"ABC"
],
"firewall_ip" : "98.87.76.655",
"firewall_type" : "abc",
"endpointURL" : "alsosomething.com",
"parserType" : "parseO365Delta.py"
}
]
}
import json
import subprocess
def processService(service):
for item in service[0].values():
print(item)
def main():
with open('config.json', 'r') as config:
config_list = json.load(config)
for services in config_list["my_services"]:
processService(services)
if __name__ == "__main__":
main()
【问题讨论】:
标签: python json dictionary for-loop iteration