2013-07-08 48 views
0

我正在使用Trello API,並且在Stackoverflow上對其他人有一個常見問題。我嘗試使用提供的解決方案,但得到一個奇怪的錯誤。無法弄清楚原因。Python,JSON TypeError

cardlist_url = "https://api.trello.com/1/boards/" + board + "$ 
r = requests.get(cardlist_url) 
r = r.text 
j = json.loads(r) 
#Check for when due 
print j['due'] 

但在打印j['due'],我得到:

TypeError: list indices must be integers, not str 

這是看似常識的賠率。順便提一句,我輸入了一些整數,並沒有任何效果! (我確實保存了!)

非常感謝,提前。

+0

'j'是一個'list'而不是'dict'。 –

回答

0

嘗試財產以後像

for i in range(len(J)): 
    print "J number " + str(i) + " = " + J[i] 

這應該給你看看裏面Ĵ

0

首先嚐試將JSON輸出轉換成詞典:

j = dict(json.loads(r)) 

,你應該能夠訪問字典的按鍵

0

什麼Ashwini喬德里說...

有效的json文件可以存在任何列表或字符串的組合。

由於json模塊工作方式的例子:

import json 

listText = '["This","Is","A","Valid","Json","File"]' 
dictText = '{"So":["Is","This"]}' 

print type(json.loads(listText)) 
print type(json.loads(dictText)) 

這只是取決於你如何閱讀JSON文件的結構。如果json文件的總體結構是一個列表,你可能會想要考慮從根本上改變了什麼。