Python: list to JSON
I am trying to use Django with jquery UI autocomplete but having trouble sending response.
Here is my code:
def ajax_tags_autocomplete(request):
""" Autocomplete for tag list """
beginning_of_title = request.GET.get('term', '')
开发者_JS百科 tags_found = Tag.objects.values_list('title', flat=True).filter(title__startswith=beginning_of_title)
return HttpResponse(json.dumps(tags_found), mimetype='application/json')
I get an error:
[u"php"] is not JSON serializable
Why? It is not possible to serialize a list? What should I pass to the serializer, then?
I would be greatful for any advice.
Are you sure it's actually a list containing unicode objects and not a list containing some database objects? The u"php"
might just be the repr()
of the object.
Try json.dumps([unicode(t) for t in tags_found])
or json.dumps(map(unicode, tags_found))
精彩评论