首页 > 解决方案 > 无法序列化 json 数组

问题描述

我对使用 API 有点陌生,并且在尝试 json.dumps api 响应时收到 graphconnection is not serializable 错误。响应看起来是一个 json 数组,但是,当我运行 Type(api_response) 时,没有任何输出。

任何帮助,将不胜感激。

我努力了Type(api_response)

api_response = api_instance.graph_user_group_members_list(group_id, content_type, accept,limit=limit, skip=skip, x_org_id=x_org_id)
#pprint(api_response)

data = json.dumps(api_response)

我想将 api_response 转换为 python dict 格式,以便我可以使用 json 值....

我收到此错误

TypeError: Object of type GraphConnection is not JSON serializable

如果我打印 api_response,这是输出

[{'_from': None, 'to': {'id': '', 'type': 'user'}}, {'_from': None, 
'to': {'id': '', 'type': 'user'}}]

标签: pythonjsonapidictionary

解决方案


我实际上能够通过使用([ob.dict for ob in api_response])找到解决方案。仍然认为我缺少一些关于 json.dumps 如何工作的知识。


推荐阅读