gpt4 book ai didi

python - 从 Python 2.7 转换到 3.4 时出现 jsonify 问题

转载 作者:行者123 更新时间:2023-11-30 23:15:51 24 4
gpt4 key购买 nike

我在 Flask 中有一个简单的 api,可以在 Python 2.7 上正常运行。 View 通过在 transform_data 字典上调用 jsonify 来返回 json 响应。

return jsonify(transform_data)

现在我正在转向 python 3.4,但出现以下错误。

raise TypeError(repr(o) + " is not JSON serializable")

TypeError: ItemsView(OrderedDict([('Car', 1217.62), ('Transfer', 551.86), 'Service Fees', 1313.6), ('MISC Costing', 82.09), ('Air', 35858.23), ('Hotel', 6179.34)])) is not JSON serializable

为什么我会收到此错误?我需要更改什么才能使其在 Python 3.4 上运行?

最佳答案

当您创建transform_data时,您将一个值设置为字典的项目。但是,.items() 现在返回字典上的 ItemsView 迭代器,而不是元组列表。找到您调用 .items() 的位置,并将结果转换为列表:list(my_dict.items())

关于python - 从 Python 2.7 转换到 3.4 时出现 jsonify 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com