使用azure-sdk-for-python获取json格式的虚拟机大小列表 [英] Get Virtual Machine sizes list in json format using azure-sdk-for-python
问题描述
我正在尝试使用azure-sdk-for-python库连接到azure云并执行某些操作.
I'm trying to use azure-sdk-for-python library to connect to azure cloud and to execute certain operations.
我遵循Azure文档中提供的以下代码示例,从API开始,该API根据位置获取虚拟机的大小.
I followed the below code samples provided in Azure docs to start with the API that get the virtual machine sizes based on a location.
For getting Authentication client: https://docs.microsoft.com/en-us/python/azure/python-sdk-azure-authenticate?view=azure-python
一旦获得ComputeManagementclient对象,以下几行将检索VirtualMachineSize对象的列表.
Once the ComputeManagementclient Object is obtained, The following lines retrieves list of VirtualMachineSize objects.
client = CompteManagementClient(credentials, subscription_id)
vmSizesList = client.virtual_machine_sizes.list()
我希望此输出为json格式而不是对象.所以我尝试了以下语句:结果= json.dumps(vmSizesList)
I want this output to be in json format instead of object. So I tried the below statement: result = json.dumps(vmSizesList)
这将导致错误对象不可序列化".
This results in error "object is not serializable".
我认为的另一种方法是仅循环并手动构建json结构化数据.任何帮助将不胜感激.
The other way I think is only to loop and to manually build json structured data. Any help would be grateful.
如果需要任何其他信息,请发表评论.
Please comment in case you need any additional info.
推荐答案
list
调用的结果返回一个可迭代对象,因此首先您必须将此可迭代对象用作列表.然后,每个对象都将包含一个 serialize
方法,该方法会将对象放回其JSON形式.
Result of a list
call return an iterable, so first you would have to consume this iterable as a list. Then, each object will contain a serialize
method that will put back the object into its JSON form.
实际上:
client = CompteManagementClient(credentials, subscription_id)
vmSizesList = [vm_size.serialize() for vm_size in client.virtual_machine_sizes.list()]
json.dumps(vmSizesList)
这篇关于使用azure-sdk-for-python获取json格式的虚拟机大小列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!