0

我正在寻找在 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' }
]}

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

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")

我不断得到list indices must be integers, not str。有任何想法吗?

4

1 回答 1

7

你有错别字...

finalJson = []

应该

finalJson = {}
于 2013-02-01T16:39:17.143 回答