Sunday, August 02, 2009

output json on django

I just finish a job in django, and I use it for some ajax part of the project. So I generate json for this. Below is a snippet sanitize from the code my job use...

try:
import json
except:
from simplejson as json
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
def generate_json(request):
#it can be a dictionary too, anything supported by simplejson
somelist=[]
#do something here, to fill in somelist
encoded=json.dumps(somelist)
response['content-disposition'] = 'attachment; filename=properties.json'
response=HttpResponse(encoded,mimetype="application/json")
return response

So there it is...

p.s I need a way to display source code properly on my blog, idea?

No comments:

Post a Comment