0
import json
from django.core import serializers
from django.http import HttpResponse, Http404
from menu.models import *

def Database(request):
    if request.method == 'GET':
       menus = Menu.objects.all()
       return toJSON(serialize(menus))

def serialize(menus):
    serialized = []
    for obj in menus:
        serialized.append(obj.serializer())
    return serialized

def toJSON(menus, status=200):
    j = json.dumps(menus, ensure_ascii=False)
    return HttpResponse(j, status=status, content_type='application/json; charset=utf-8')

views.py喜欢这样,但它只返回[],我该如何解决?

4

1 回答 1

0

你的意思是你没有得到键值对,而是得到了 list ?

利用

j = json.dumps({"menus":menus}, ensure_ascii=False)

代替

j = json.dumps(menus, ensure_ascii=False)
于 2012-09-30T11:18:47.393 回答