Reputation: 357
I am developing a user interface with Django. I have performed some operations in the views.py. and i have retrieved values which looks like this.
results = [{'start_time': '23:51:58', 'id_start_time': datetime.time(23, 51, 58)}, {'start_time': '23:59:04', 'id_start_time': datetime.time(23, 59, 4)}]
i am trying to save this into a json object, to be transferred back to the HTML page, where i have JQuery syntax to perform operations on these values, but for some reason its not working.
The syntax i used is,
import json
json_var = json.dumps(results)
i have also used syntax of,
variable_result = simplejson.dumps(results)
But both of them does not seem to work or i may not have understood the logic of how it works. If the syntax is correct, can u please tell me how to view those stored objects. If the syntax is not correct, can someone please tell me how it should work with a perfect code.
Upvotes: 0
Views: 335
Reputation: 13496
TypeError: datetime.time(23, 51, 58) is not JSON serializable
Serialize/Stringify the datetime object first before using json.dumps
or simplejson.dumps
. Python's integrated json serializer can not work with datetime "types", just with built-in types like int
, float
, str
, etc..
Upvotes: 0
Reputation: 1869
from django.core import serializers
json_serializer = serializers.get_serializer("json")
response = json_serializer.serialize(list, ensure_ascii=False, indent=2, use_natural_keys=True)
return HttpResponse(response, mimetype="application/json")
Upvotes: 1