【问题标题】:How can I replace simplejson with json in django python?如何在 django python 中用 json 替换 simplejson?
【发布时间】:2012-10-11 21:44:21
【问题描述】:

我的视图中有以下代码:

def __init__(self, obj='', json_opts={}, mimetype="application/json", *args, **kwargs):
        content = simplejson.dumps(obj, **json_opts)
        super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs)

由于simplejson 将被弃用,我可以使用它吗

content = json.dumps(obj, **json_opts)

还是我需要做更多?

【问题讨论】:

标签: python django


【解决方案1】:

改用python的json:

Django 1.5 release notes

import json

【讨论】:

    【解决方案2】:

    根据this answerjsonsimplejson。但是,根据这个release note,可能会有一些incompatibilities,具体取决于您当前使用的simplejson 版本。无论哪种方式,您都需要在某个时候将simplejson 替换为json。只需确保在将代码投入生产之前对其进行测试即可。

    【讨论】:

    • 不幸的是,事情没有那么简单。请阅读问题 cmets 中链接中 Django 文档中链接到的错误报告。
    • 感谢您指出这一点。我更新了我的答案以反映可能的不兼容性。
    猜你喜欢
    • 2011-01-02
    • 2012-05-17
    • 1970-01-01
    • 2011-01-16
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2020-10-23
    相关资源
    最近更新 更多