【问题标题】:How to convert here myobject into json? Here type of myobject is <class 'oci.identity.models.compartment.Compartment'>如何在这里将 myobject 转换为 json?这里 myobject 的类型是 <class 'oci.identity.models.compartment.Compartment'>
【发布时间】:2021-05-20 07:44:03
【问题描述】:

这个函数给出了一个租户中的所有隔间直到它的结束点。这里myobject的类型是 如何将其转换为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))

'''

【问题讨论】:

    标签: python json types oracle-cloud-infrastructure json-serializable


    【解决方案1】:

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

    谢谢 阿努潘

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

    【讨论】:

    • 让我们说变量是简单的 myobject 而不是 cjson 那么它可能是什么。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2010-09-17
      • 2021-08-06
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多