gpt4 book ai didi

python - 如何使用 Python 从另一个对象创建新的 JSON 对象?

转载 作者:行者123 更新时间:2023-12-01 02:24:50 26 4
gpt4 key购买 nike

我需要创建一个比原始信息更少的 JSON 对象:

从 api 服务获取原始 JSON 对象后,它看起来像这样:

{
"queryResponse": {
"@type": "AccessPointDetails",
"@rootUrl": "https://xxx/webacs/api/v1/data",
"@requestUrl": "https://xxx/webacs/api/v1/data/AccessPointDetails?.full=true&.firstResult=250&.maxResults=2",
"@responseType": "listEntityInstances",
"@count": "347",
"@first": "250",
"@last": "251",
"entity": [
{
"@url": "https://xxx/webacs/api/v1/data/AccessPointDetails/1897332",
"@type": "AccessPointDetails",
"@dtoType": "accessPointDetailsDTO",
"accessPointDetailsDTO": {
"@id": "1897332",
"@displayName": "1897332",
"adminStatus": "ENABLE",
"apType": "AP1140",
"clientCount": 1,
"clientCount_2_4GHz": 1,
"clientCount_5GHz": 0,
"ethernetMac": "xxx",
"ipAddress": "xxx",
"locationHeirarchy": "Root Area",
"macAddress": "xxx",
"mapLocation": "xxx",
"model": "AIR-LAP1141N-A-K9",
"name": "xxx",
"serialNumber": "xxx",
"softwareVersion": "8.0.140.0",
"status": "CLEARED",
"type": "UnifiedAp",
"unifiedApInfo": {
"instanceId": 0,
"instanceVersion": 0,
"apCertType": 1,
"apGroupName": "xxx",
"apMode": 2,
"apStaticEnabled": 0,
"bootVersion": "12.4.23.6",
"capwapJoinTakenTime": 1500,
"capwapUpTime": 52444681,
"controllerIpAddress": "xxx",
"controllerName": "wlc_5508",
"contryCode": "US",
"encryptionEnabled": false,
"flexConnectMode": false,
"iosVersion": "15.3(3)JA10$",
"linkLatencyEnabled": false,
"poeStatus": 5,
"portNumber": 13,
"powerInjectorState": 1,
"preStandardState": 0,
"primaryMwar": "xxx",
"rogueDetectionEnabled": true,
"sshEnabled": false,
"statisticsTimer": 180,
"telnetEnabled": false,
"vlanEnabled": true,
"vlanNativeId": 16,
"WIPSEnabled": 0,
"wlanVlanMappings": {
"wlanVlanMapping": {
"ssid": "xxx",
"vlanId": 220,
"wlanId": 3
}
}
},
"upTime": 2666643681
}
},
{
"@url": "https://xxx/webacs/api/v1/data/AccessPointDetails/1897334",
"@type": "AccessPointDetails",
"@dtoType": "accessPointDetailsDTO",
"accessPointDetailsDTO": {
"@id": "1897334",
"@displayName": "1897334",
"adminStatus": "ENABLE",
"apType": "AP3500E",
"clientCount": 8,
"clientCount_2_4GHz": 8,
"clientCount_5GHz": 0,
"ethernetMac": "xxx",
"ipAddress": "xxx",
"locationHeirarchy": "Root Area",
"macAddress": "xxx",
"mapLocation": "xxx",
"model": "AIR-CAP3501E-A-K9",
"name": "xxx",
"serialNumber": "xxx",
"softwareVersion": "8.1.131.0",
"status": "CLEARED",
"type": "UnifiedAp",
"unifiedApInfo": {
"instanceId": 0,
"instanceVersion": 0,
"apCertType": 1,
"apGroupName": "xxx",
"apMode": 2,
"apStaticEnabled": 0,
"bootVersion": "15.3.2.4",
"capwapJoinTakenTime": 1500,
"capwapUpTime": 52445240,
"controllerIpAddress": "xxx",
"controllerName": "wlc_5520",
"contryCode": "US",
"encryptionEnabled": false,
"flexConnectMode": false,
"iosVersion": "15.3(3)JBB6$",
"linkLatencyEnabled": false,
"poeStatus": 5,
"portNumber": 8,
"powerInjectorState": 1,
"preStandardState": 0,
"primaryMwar": "Cisco_10:2d:ae",
"rogueDetectionEnabled": true,
"sshEnabled": false,
"statisticsTimer": 180,
"telnetEnabled": false,
"vlanEnabled": true,
"vlanNativeId": 21,
"WIPSEnabled": 0,
"wlanVlanMappings": {
"wlanVlanMapping": {
"ssid": "xxx",
"vlanId": 220,
"wlanId": 1
}
}
},
"upTime": 2399985140
}
}
]
}
}

我需要创建一个新的 JSON 对象,其中仅包含上面的一些信息,如下所示:

"entity": [
{
"@id": 12345,
"name": asdf,
"clienCount": 5,
"clientCount_2_4GHz": 3,
"clientCount_5GHz": 2
},
{
"@id": 12345,
"name": asdf,
"clienCount": 5,
"clientCount_2_4GHz": 3,
"clientCount_5GHz": 2
}
]

如果你看一下,实体列表位于原始数据的字典内。

我尝试查找相关信息,但只找到了如何删除或如何每次提取 1 条数据的信息。

如何在 Python 中执行此操作?

问候。

编辑:重复的帖子?如果是这样,请您不要标记它,而是链接到答案,因为我花了几个小时寻找答案? (不是袁吉,感谢您指出单独的资源)

最佳答案

您的问题只是 python getting a list of value from list of dict 的组合和 Filter dict to contain only certain keys?

通过使用这两个问题的解决方案,您可以实现它:

假设 s 是从 json 数据转换而来的 dict

keys = ['@id', 'name', 'clientCount', 'clientCount_2_4GHz', 'clientCount_5GHz']
new_dict = {'entity': [{key: d['accessPointDetailsDTO'][key] for key in keys} for d in s['queryResponse']['entity']]}

然后使用 json.dumps 转换 new_dict :

your_json_object = json.dumps(new_dict)

关于python - 如何使用 Python 从另一个对象创建新的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com