【发布时间】:2021-05-06 09:44:31
【问题描述】:
我正在尝试访问 JSON 响应,我正在尝试使用 get() 方法,但我收到“对象没有属性 'get'”错误,当我使用 [] 尝试时,我得到了“对象不可下标”错误。 响应是有效的,因为我在 get 操作之前打印了它,一切看起来都很好。 之前解析响应没有这样的问题。
def get_return_params_and_identifiers(return_params):
print("******** res start **********")
print(return_params)
print("******** res end **********")
return_parameters = return_params.get('return_parameters')
这是回复的一部分:
******** res start **********
{'levels': None,
'return_parameters': [{'identifier_name': 'Premium',
'level': None,
'return_parameter_name_pk': 1258407,
'return_parameters_details': [{'base_parameter_name': 'Premium '
'Parameter',
'base_parameter_pk': 1149913,
'class_value': None,
....
....
有没有人遇到过这种情况并知道发生了什么。
编辑:
尝试使用 json.loads() 但响应是 DTO 而不是 JSON,因此出现另一个错误: TypeError: JSON 对象必须是 str、bytes 或 bytearray,而不是 DTO
DTO 类如下:
class ReturnParametersContainerDTO(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
"""
Attributes:
swagger_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
"""
swagger_types = {
'return_parameters': 'list[ReturnParameterDTO]',
'levels': 'LevelsDTO'
}
attribute_map = {
'return_parameters': 'returnParameters',
'levels': 'levels'
}
它清楚地表明类中的项目是字典,但不知何故它无法识别它。
【问题讨论】:
-
print(type(return_params))的结果是什么? -
Type:
我已经用这个 DTO 更新了帖子