2013-02-01 24 views
0

我期待建立在Django以下JSON(我用DataGrid工作):Django和JSON

{ 
    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。有任何想法嗎?

+2

請注意,您的例子實際上不是有效的JSON:你必須使用雙引號鍵和值。 –

回答

7

你有一個錯字...

finalJson = [] 

應該

finalJson = {} 
+0

我已更正我的問題 – Sevenearths

+0

您是對的。現在所有的作品:) – Sevenearths