Django和JSON

Sev*_*ths 0 python django dojo json

我想在Django中创建以下JSON(我正在使用a DataGrid):

{
    identifier: 'id',
    label: 'name',
    items: [
            { id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',
                    timezone: '-1 UTC to +4 UTC',
                    children:[{_reference:'EG'}, {_reference:'KE'}, {_reference:'SD'}] },
                { id: 'EG', name:'Egypt', type:'country' },
                { id: 'BR', name:'Brazil', type:'country', population:'186 million' },
                { id: 'AR', name:'Argentina', type:'country', population:'40 million' }
]}
Run Code Online (Sandbox Code Playgroud)

我现在正在做这样的事情:

filesJson = []
for index,lv in enumerate(letterList):
    printed = ''
    if lv.letter.received:
        inout = '<span class="..."></span>'
    else:
        inout = '<span class="..."></span>'
    if lv.printed_last:
        printed = '<span class="..."></span>'
    filesJson.append({'id':str(index),
                      'letterID':lv.letter.id,
                      'position':str(index).zfill(4),
                      'inout':inout,
                      'dateH':lv.humanReadableCreated(),
                      'date':lv.created.strftime('%d/%m/%y'),
                      'time':lv.created.strftime('%H:%M'),
                      'user':lv.created_by.username,
                      'name':lv.name(),
                      'printed':printed})

finalJson = {}
finalJson['id'] = 'id'
finalJson['label'] = 'name'
finalJson['items'] = filesJson
return HttpResponse(simplejson.dumps(finalJson), mimetype="application/json")
Run Code Online (Sandbox Code Playgroud)

我一直在list indices must be integers, not str.有任何想法吗?

Tom*_*tie 7

你有一个错字......

finalJson = []
Run Code Online (Sandbox Code Playgroud)

应该

finalJson = {}
Run Code Online (Sandbox Code Playgroud)