0

此函数将一个租户中的所有隔间提供到其终点。这里 myobject 的类型是 <class 'oci.identity.models.compartment.Compartment'> 如何将其转换为 json?这是哪种类型?在使用 json.dumps() 时,它显示类型 Compartment 不是 JSON 可序列化的。

'''

import oci
import hashlib

import uuid
import json

global finale_response,tenant_id,profile
profile="abc-profile"
tenant_id = "ocid1.tenancy.oc1.."
finale_response = []

config = oci.config.from_file(profile_name=profile)

def get_compartments(id):
    identity_client = oci.identity.IdentityClient(config)
    list_compartments_response = identity_client.list_compartments(compartment_id=id)
    response = list_compartments_response.data
    if response != []:
    
        child_compartment_list = []

        for i in range(len(response)):
            compartmentId = response[i].id
            myobject= response[i]

            print(type(myobject))
        
            finale_response.append({"profileName":profile,
                              "compartmentId":compartmentId,
                              "tenantId": tenant_id,
                              "comMetadata":myobject})

            child_compartment_list.append(compartmentId)

        for id in child_compartment_list:
            get_compartments(id)

    return finale_response

myList = get_compartments(tenant_id)
print(myList)
print(type(myList))

'''

4

2 回答 2

1

有关如何获取 Json 响应的示例,请参阅:https ://github.com/oracle/oci-python-sdk/blob/master/examples/json_response_example.py。

于 2021-05-25T18:57:29.557 回答
1

请查看此页面此页面,这可能有用[使用 cJSON 创建 JSON 字符串][1]

谢谢阿努帕姆

[1]:https: //www.programmersought.com/article/49402068665/#:~:text= char cJSON_Print(cJSON%20 item,of%20this%20object%20to%20the.

于 2021-05-20T08:42:23.963 回答