无法弄清楚如何将JSON附加到单个父对象中

时间:2019-05-04 04:37:38

标签: python json

我遇到了一个无法解决的问题。我正在尝试构建一个JSON对象。我正在使用某些VMware API,正在构建用于用户名的JSON对象,并将VM信息嵌套在其中。我在构建嵌套对象时遇到了麻烦。请参阅下文,我将进一步解释。注意,注释用作标记以标识虚拟机的所有者。

owner_logged_in = "johndoe"
service_instance = connect.SmartConnectNoSSL(host='10.0.0.202', user='', pwd='')
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
container = content.rootFolder  # starting point to look into
viewType = [vim.VirtualMachine]  # object types to look for
recursive = True  # whether we should look into it recursively
containerView = content.viewManager.CreateContainerView(container, viewType, recursive)
children = containerView.view

virtual_machines = []
vm_username = {}
vm_container = {}
for child in children:
    summary = child.summary
    annotation = summary.config.annotation
    if owner_logged_in == annotation:
        children = []
        children.append({'ip': summary.guest.ipAddress,'power': summary.runtime.powerState})
        vm_container['name'] = summary.config.name
        vm_username[owner_logged_in] = vm_container
        vm_container['properties'] = children
        jsonvalues = json.dumps(vm_username)
        #debug#
        print(jsonvalues)
        #debug#

返回的结果如下:

 {"johndoe": {"name": "centos01", "properties": [{"ip": "10.0.0.201", "power": "poweredOn"}]}}
 {"johndoe": {"name": "dc01", "properties": [{"ip": "10.0.0.200", "power": "poweredOn"}]}}

我需要以某种方式将这两个对象合并为一个对象,然后将其存储在一个值中,然后使用DJANGO Web应用程序迭代该值,以使用一些javascript(tabullar.js)构建表。我可以处理那部分,但我正在努力的是想出一种方法来制作一个对象。一个用户可能有多个虚拟机,我需要每个虚拟机的属性,但让“ johndoe”成为父级。

基本上,我需要将其格式化为使用tabular.js正确将其转换为表格的形式。

{  
  "johndoe":[  
     {  
        "name":"centos01",
        "properties":[  
           {  
              "ip":"10.0.0.201",
              "power":"poweredOn"
           }
        ]
     },
     {  
        "name":"dc01",
        "properties":[  
           {  
              "ip":"10.0.0.200",
              "power":"poweredOn"
           }
        ]
     }
  ]
}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

尝试关注:

from collections import defaultdict

virtual_machines = []
vm_username = defaultdict(list)
vm_container = defaultdict(list)
for child in children:
    summary = child.summary
    annotation = summary.config.annotation
    if owner_logged_in == annotation:
        children = []
        children.append({'ip': summary.guest.ipAddress,'power': summary.runtime.powerState})
        vm_container['name'] = summary.config.name
        vm_username[owner_logged_in].append(vm_container)
        vm_container['properties'] = children

jsonvalues = json.dumps(vm_username, indent=4)
#debug#
print(jsonvalues)
#debug#

答案 1 :(得分:1)

下面的解决方案应该可以代替打印,只要您可以将其添加到列表中即可。

import json
d_list=[
{"johndoe": {"name": "centos01", "properties": [{"ip": "10.0.0.201", "power": "poweredOn"}]}},
{"johndoe": {"name": "dc01", "properties": [{"ip": "10.0.0.200", "power": "poweredOn"}]}},
{"janedoe": {"name": "centos02", "properties": [{"ip": "10.0.0.201", "power": "poweredOn"}]}},
{"janedoe": {"name": "dc02", "properties": [{"ip": "10.0.0.200", "power": "poweredOn"}]}}
]
d_new={name:[v for x in d_list for k,v in x.items() if k ==name] for name in set(list(y)[0] for y in d_list)}
# for printing output properly
print(json.dumps(d_new,indent=4))

输出

{
    "johndoe": [
        {
            "name": "centos01",
            "properties": [
                {
                    "ip": "10.0.0.201",
                    "power": "poweredOn"
                }
            ]
        },
        {
            "name": "dc01",
            "properties": [
                {
                    "ip": "10.0.0.200",
                    "power": "poweredOn"
                }
            ]
        }
    ],
    "janedoe": [
        {
            "name": "centos02",
            "properties": [
                {
                    "ip": "10.0.0.201",
                    "power": "poweredOn"
                }
            ]
        },
        {
            "name": "dc02",
            "properties": [
                {
                    "ip": "10.0.0.200",
                    "power": "poweredOn"
                }
            ]
        }
    ]
}