0

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.

Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
sankar
  • 347
  • 3
  • 4
  • 15
  • If you use `return HttpResponse(simplejson.dumps(results), mimetype="application/json")` to send the data, then it is the correct syntax. How are you parsing this on the client side? – Scillon Aug 08 '12 at 07:50
  • hi can you please this link: where i have explained much more detailed manner : http://stackoverflow.com/questions/11860183/dynamic-jquery-view-in-django – sankar Aug 08 '12 at 08:04

2 Answers2

1
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")

Please check this or this

Community
  • 1
  • 1
Nick
  • 1,799
  • 3
  • 23
  • 32
0
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..

Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • thank you. it is working. the error u have told me is the same. Once i converted it into string, i met the desired functionality – sankar Aug 08 '12 at 09:34