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.

2
  • 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? Commented Aug 8, 2012 at 7:50
  • hi can you please this link: where i have explained much more detailed manner : stackoverflow.com/questions/11860183/… Commented Aug 8, 2012 at 8:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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..

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.