python - Django is producing a useless JSON object from objects.all() -


i've built django view sake of returning pure json object:

from django.core import serializers import json def testjson(request):     all_objects = list(message.objects.all())      to_json = serializers.serialize('json', all_objects)      return httpresponse(json.dumps(to_json), mimetype='application/json') 

to_json above ends looking this:

\"employees\": [ { \"firstname\":\"john\" , \"lastname\":\"doe\" },  { \"firstname\":\"anna\" , \"lastname\":\"smith\" },  { \"firstname\":\"peter\" , \"lastname\":\"jones\" } ] 

this useless \ , can't figure out how rid of them. i've tried \ triggers escape character:

to_json = to_json.replace('\', '') 

how change json object replace \" "?

you don't need call json.dumps(), serialize() make json string response:

from django.core import serializers  def testjson(request):     data = serializers.serialize('json', message.objects.all())     return httpresponse(data, mimetype='application/json') 

also see:


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -