2014-11-04 92 views
0

我得到這個短結果當過功能processOutput(output)被稱爲大廈嵌套的JSON對象

output = {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''} 

假設這個函數調用三次,然後導致JSON看起來應該像

{ 'object[0]' : {'LEND': '0', 'LINK': 'Xx', 'REND': '4', 'RULE': ''}, 
    'object[1]' : {'LEND': '0', 'LINK': 'Xa', 'REND': '3', 'RULE': 'two'}, 
    'object[2]' : {'LEND': '0', 'LINK': 'Xz', 'REND': '12', 'RULE': ''} 
} 

,這樣我可以訪問json的值如下object[0]['LINK']等等

def printOutput(self, processed_output): 
    #processed_output is output. 
+1

請問你試過什麼? – Nilesh 2014-11-04 09:28:28

回答

1

你應該定義一個計數器並輸出爲全局變量並追加以輸出每個結果。

output = {} 
c = 0 

def printOutput(self, processed_output): 
    global c, output 
    output['object[%i]' % c] = processed_output 
    c += 1 
+0

謝謝,但它給'NameError:全局名稱'結果'未定義' – puncrazy 2014-11-04 09:58:29

+0

嗯,我沒有看到問題的任何部分'結果',也許你需要檢查你的代碼。 – 2014-11-04 10:01:36

+0

'output'就是我的結果 – puncrazy 2014-11-04 10:19:51