将数据结构转储到Python中的JSON会引发无序类型str和内置的\u function_or_method

2024-05-19 13:33:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在数据结构上调用Flask的jsonify,但是我得到了TypeError: unorderable types: str() < builtin_function_or_method()。如何修复此错误?在

bucketlists = [{
    'id': 1,
    'name': "BucketList1",
    'items': [{
        id: 1,
        'name': "I need to do X",
        'date_created': "2015-08-12 11:57:23",
        'date_modified': "2015-08-12 11:57:23",
        'done': False
    }],
    'date_created': "2015-08-12 11:57:23",
    'date_modified': "2015-08-12 11:57:23",
    'created_by': "1113456"
}]

@app.route('/bucketlists/', methods=['GET'])
def get_bucketlists():
    return jsonify({'bucketlists': bucketlists})

Tags: nameidflask数据结构datefunctiontypesmodified
1条回答
网友
1楼 · 发布于 2024-05-19 13:33:32

^{}是一个内置的Python函数;jsonify无法序列化它,您需要用引号将字典键包装起来,使其成为字符串:

bucketlists = [{
    'id': 1,
    'name': "BucketList1",
    'items': [{
        'id': 1, #   -> Here
        'name': "I need to do X",
        'date_created': "2015-08-12 11:57:23",
        'date_modified': "2015-08-12 11:57:23",
        'done': False
    }],
    'date_created': "2015-08-12 11:57:23",
    'date_modified': "2015-08-12 11:57:23",
    'created_by': "1113456"
}]

此外,还需要添加双下划线才能访问模块的名称:

^{pr2}$

相关问题 更多 >