Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python3大型json解析键错误_Python_Json_Python 3.x - Fatal编程技术网

python3大型json解析键错误

python3大型json解析键错误,python,json,python-3.x,Python,Json,Python 3.x,尝试使用Python3.6解析json时,我遇到了困难。我需要得到所有的卷数。我一直能够解析到“容器”,之后我遇到了麻烦 def get_dc(): auth_token = os.environ['OCPTOKEN'] url = os.environ['OCPURL']+"/apis/apps.openshift.io/v1/deploymentconfigs" header = {"Authorization": "bearer " + auth_token}

尝试使用Python3.6解析json时,我遇到了困难。我需要得到所有的卷数。我一直能够解析到“容器”,之后我遇到了麻烦

def get_dc():
    auth_token = os.environ['OCPTOKEN']
    url = os.environ['OCPURL']+"/apis/apps.openshift.io/v1/deploymentconfigs"
    header = {"Authorization": "bearer " + auth_token}
    response = requests.get(url,headers=header,verify=False)
    if(response.ok):
            dc_list = json.loads(response.content)
            name = []
            vol_list = []
            vol_name = []
            vol_path = []
            items = dc_list['items']
            for item in items:
                    name.append(item['metadata']['name'])
                    vol_list = json.dumps(item['spec']['template']['spec']['containers'])
                    vol_list2 = json.loads(vol_list)
                    for vol in vol_list:
                          vol_name.append(vol['volumeMounts'][0]['name'])
                          vol_path.append(vol['volumeMounts']['mountPath'])

            return json.dumps(vol_name, sort_keys=True, indent=4)
            #return vol_list2.keys()
    else:
            return response.raise_for_status()`
JSON

当我尝试调用volumeMounts时,我收到以下消息:

 Traceback (most recent call last):
  File "test.py", line 96, in <module>
    output = get_dc()
  File "test.py", line 52, in get_dc
    vol_name.append(vol['volumeMounts']['name'])
  TypeError: string indices must be integers
回溯(最近一次呼叫最后一次):
文件“test.py”,第96行,在
输出=get_dc()
get_dc中第52行的文件“test.py”
vol_name.append(vol['volumeMounts']['name'])
TypeError:字符串索引必须是整数
任何帮助都将不胜感激

更新:
添加了代码,尝试使用vol_name.append(vol['volumeMounts'][0]['name']),仍然可以在JSON中获取字符串索引必须是整数。您提供的
volumeMounts
键包含一个包含字典的列表。请尝试以下操作:

vol_name.append(vol['volumeMounts'][0]['name'])

正如我从JSON的以下部分了解到的,volumeMounts是一个数组,而不是dict

“卷数”:[ { “mountPath”:“/var/lib/pgsql/data”, “名称”:“postgresql数据” } ]

如果是这样的话,应该有
vol\u name.append(vol['volumeMounts'][0]['name'])
找到了解决方案,对vol\u列表使用.get()。工作职能:

def get_dc():
    auth_token = os.environ['OCPTOKEN']
    url = os.environ['OCPURL']+"/apis/apps.openshift.io/v1/deploymentconfigs"
    header = {"Authorization": "bearer " + auth_token}
    response = requests.get(url,headers=header,verify=False)
    if(response.ok):
            dc_list = json.loads(response.content)
            name = []
            vol_list = []
            vol_name = []
            vol_path = []
            items = dc_list['items']
            for item in items:
                    name.append(item['metadata']['name'])                        vol_list = item['spec']['template']['spec']['containers'][0]
                    for vol in vol_list.get('volumeMounts', []):
                          vol_name.append(vol['name'])
                          vol_path.append(vol['mountPath'])

            return json.dumps(vol_name, sort_keys=True, indent=4)
            #return vol_list2.keys()
    else:
            return response.raise_for_status()

如果您共享python代码会有所帮助。根据您的错误,这完全取决于您如何定义
vol
。要使其像您编写的那样工作,
vol
必须是容器列表中的一个。添加了我的代码,感谢您的响应添加了我的代码,现在看起来很混乱,因为我一直在解决此问题。感谢您的回复,我尝试添加[0],看起来它仍然在抱怨字符串索引必须是“volumeMounts”的整数。做vol['name']给了我正确的回答,“sonarqube”,但是volumeMounts我仍然有一个问题。
def get_dc():
    auth_token = os.environ['OCPTOKEN']
    url = os.environ['OCPURL']+"/apis/apps.openshift.io/v1/deploymentconfigs"
    header = {"Authorization": "bearer " + auth_token}
    response = requests.get(url,headers=header,verify=False)
    if(response.ok):
            dc_list = json.loads(response.content)
            name = []
            vol_list = []
            vol_name = []
            vol_path = []
            items = dc_list['items']
            for item in items:
                    name.append(item['metadata']['name'])                        vol_list = item['spec']['template']['spec']['containers'][0]
                    for vol in vol_list.get('volumeMounts', []):
                          vol_name.append(vol['name'])
                          vol_path.append(vol['mountPath'])

            return json.dumps(vol_name, sort_keys=True, indent=4)
            #return vol_list2.keys()
    else:
            return response.raise_for_status()