相关疑难解决方法(0)

如何使类JSON可序列化

如何使Python类可序列化?

一个简单的课程:

class FileItem:
    def __init__(self, fname):
        self.fname = fname
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能得到输出:

>>> import json

>>> my_file = FileItem('/foo/bar')
>>> json.dumps(my_file)
TypeError: Object of type 'FileItem' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

没有错误(__CODE__)

python serialization json

730
推荐指数
19
解决办法
61万
查看次数

Python:获取异常的错误消息

在python 2.6.6中,我如何捕获异常的错误消息.

IE:

response_dict = {} # contains info to response under a django view.
try:
    plan.save()
    response_dict.update({'plan_id': plan.id})
except IntegrityError, e: #contains my own custom exception raising with custom messages.
    response_dict.update({'error': e})
return HttpResponse(json.dumps(response_dict), mimetype="application/json")
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.我明白了:

IntegrityError('Conflicts are not allowed.',) is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

python django exception-handling

33
推荐指数
1
解决办法
4万
查看次数

标签 统计

python ×2

django ×1

exception-handling ×1

json ×1

serialization ×1