我在一个名为result的python字典中有这个部分[{'scenariotestinputid': 1, 'scenarioid_id': 1, 'testinputid_id': 1}]。我打电话的时候dumps.json文件(结果)我得到一个TypeError,说[{'scenariotestinputid':1,'scenarioid_id':1,'testinputid_id':1}]不是JSON可序列化的。有人能解释一下为什么吗?在
编辑1:完整的“结果”字典内容是:result: {'scenarios': [{'inputs': [{'scenariotestinputid': 1, 'scenarioid_ id': 1, 'testinputid_id': 1}], 'scenarioid': 1, 'scenarioname': u'Default Scenario', 'testid_id': 1, 'isdefault': 1}, {'inputs': [], 'scenarioid': 2, 'scenarioname': u'Scenario-1', 'testid_id': 1, 'isdefault': 0}], 'status': 'success'}
我使用的代码是:
^{pr2}$
函数内部
编辑2:回溯如下:(Pdb)
> /opt/my_apps/cpr_proj/cpr/views.py(512)set_benchmark_metrics_dispatcher()
-> return HttpResponse(json.dumps(result), content_type="application/json")
(Pdb)
TypeError: TypeErro...izable",)
> /opt/my_apps/cpr_proj/cpr/views.py(512)set_benchmark_metrics_dispatcher()
-> return HttpResponse(json.dumps(result), content_type="application/json")
(Pdb) bt
/opt/Python-2.7.3/Lib/threading.py(524)__bootstrap()
-> self.__bootstrap_inner()
/opt/Python-2.7.3/Lib/threading.py(551)__bootstrap_inner()
-> self.run()
/opt/Python-2.7.3/Lib/threading.py(504)run()
-> self.__target(*self.__args, **self.__kwargs)
/opt/Python-2.7.3/Lib/SocketServer.py(582)process_request_thread()
-> self.finish_request(request, client_address)
/opt/Python-2.7.3/Lib/SocketServer.py(323)finish_request()
-> self.RequestHandlerClass(request, client_address, self)
/usr/local/lib/python2.7/site-packages/django/core/servers/basehttp.py(139)__init__()
-> super(WSGIRequestHandler, self).__init__(*args, **kwargs)
/opt/Python-2.7.3/Lib/SocketServer.py(638)__init__()
-> self.handle()
/opt/Python-2.7.3/Lib/wsgiref/simple_server.py(124)handle()
-> handler.run(self.server.get_app())
/opt/Python-2.7.3/Lib/wsgiref/handlers.py(85)run()
-> self.result = application(self.environ, self.start_response)
/usr/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py(67)__call__()
-> return self.application(environ, start_response)
/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py(241)__call__()
-> response = self.get_response(request)
/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py(111)get_response()
-> response = callback(request, *callback_args, **callback_kwargs)
> /opt/my_apps/cpr_proj/cpr/views.py(512)set_benchmark_metrics_dispatcher()
-> return HttpResponse(json.dumps(result), content_type="application/json")
/opt/Python-2.7.3/Lib/json/__init__.py(231)dumps()
-> return _default_encoder.encode(obj)
/opt/Python-2.7.3/Lib/json/encoder.py(201)encode()
-> chunks = self.iterencode(o, _one_shot=True)
/opt/Python-2.7.3/Lib/json/encoder.py(264)iterencode()
-> return _iterencode(o, 0)
/opt/Python-2.7.3/Lib/json/encoder.py(178)default()
-> raise TypeError(repr(o) + " is not JSON serializable")