我有 json 格式的文件,'datastores.json',看起来像这样:
{
"{'ESXi_Host': 'elvis.lab.vsphere.com'}": {
"elvis.data": {
"capacity": 293131517952,
"uuid": "57431578-630f1322-7bf2-00212883a5b0",
"vmfs_version": "5.60",
"ssd": false,
"extents": [
"mpx.vmhba1:C0:T1:L0"
],
"local": true
我在上面运行以下代码:
import json
with open("C:\PyVmomi_out\\datastores.json") as json_file:
datastores = json.loads(json_file.read())
for dstor in datastores:
esx_host = dstor['ESXi_Host']
datastore = dstor['datastore']
我收到以下错误:
TypeError: string indices must be integers
在这条线上:
esx_host = dstor['ESXi_Host']
我知道它需要一个整数。从我一直在做的阅读来看,如果我加入
'json.loads'
代替
'json.load'
并且还替换了
'(json_file.read())'
代替
'(json_file)'
然后它将文件作为字符串读取并允许字符串解析而不是整数。为什么这不起作用?