【问题标题】:Getting Django object "is not JSON serializable"获取 Django 对象“不是 JSON 可序列化的”
【发布时间】:2014-09-21 12:38:38
【问题描述】:

我有一个读取 JSON 对象的网络服务,它给了我 unicodeEncodeError 异常。谷歌搜索了一下,我看到了How can I convert a dict to a unicode JSON string?

(我关注了其他与 unicodeEncodeError 相关的问题,但我仍然收到 AttributeError: 'dict' object has no attribute 'content')

我做了那个特定问题中提到的事情,现在我得到 ..... is not JSON serializable

谁能告诉我现在该怎么办?

以下是我的代码:

def someMethod()
    some_data = request.data
    json_string1 = json.dumps(some_data)           #GETTING ERROR ON THIS LINE
    json_string2 = get_string(json_string1)
    archive = call.send_json(json_string2)


def get_string(value):
    find_unicode = re.compile('\\\\u([\da-f]{4})')

    def get_parsed_unicode(x):
        return chr(int(x.group(1), 16))
    return find_unicode.sub(get_parsed_unicode, str(value))

感谢您的帮助!!

【问题讨论】:

  • request.data 包含什么? print request.dataprint type(request.data)
  • @laffuste: 'print request.data' 返回在 Web 服务中作为字符串传递的 JSON,'print type(request.data) 返回''跨度>

标签: python json django serialization


【解决方案1】:

将对象传递给方法时,请使用foo.serealize(true)。包括 JQUERY 以使用它。

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2015-02-24
    • 2014-10-23
    • 2018-09-12
    • 1970-01-01
    • 2016-04-01
    • 2012-07-02
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多