2017-08-29 45 views
-1

的Python nonetype錯誤所以我的JSON數據看起來像下面。我的代碼行:print(jsonData["orderData"]["txnType"])打印出來的字SALE然後我得到一個錯誤說TypeError: 'NoneType' object is not subscriptable 隨着我的理解爲什麼我得到這個錯誤,如果值是明顯存在並打印?當值存在蟒蛇

{ 
    'orderData':{ 
     'date':'2017-08-29T12:55:19-07:00', 
     'receipt':'A2ZC5N96', 
     'promo':{ 
     '@nil':'true' 
     }, 
     'pmtType':'PYPL', 
     'txnType':'SALE', 
     'item':'37', 
     'amount':'104.28', 
     'site':'PASSIOPROD', 
     'affi':'BCPATRON2', 
     'country':'US', 
     'state':'OH', 
     'lastName':{ 
     '@nil':'true' 
     }, 
     'firstName':{ 
     '@nil':'true' 
     }, 
     'currency':'USD', 
     'email':{ 
     '@nil':'true' 
     }, 
     'zip':'43206', 
     'rebillAmount':'97.00', 
     'processedPayments':'1', 
     'futurePayments':'998', 
     'nextPaymentDate':'2017-09-29T12:55:19-07:00', 
     'status':'ACTIVE', 
     'accountAmount':'44.09', 
     'role':'AFFILIATE', 
     'customerDisplayName':{ 
     '@nil':'true' 
     }, 
     'title':'aaa', 
     'recurring':'true', 
     'physical':'false', 
     'customerRefundableState':'REFUNDABLE' 
    } 
} 

全碼:

devKeys = ["KEUYS"] 
apiKeys = ["API"] 
sales = [] 
refunds = [] 
totalSales = [] 
x = 0 

while x < len(devKeys): 
y = 0 
indSale = 0 
indRefund = 0 
indTotal = 0 
totalTransactions = 0 

payload = devKeys[x]+":"+apiKeys[x] 
headers = {"Accept": "application/json", "Authorization": payload} 
r = requests.get('https://api.clickbank.com/rest/1.3/orders/list', headers=headers) 
jsonData = json.loads(r.text) 
text = r.text 
if ":[" not in text: #This line is here because the JSON looks different if theres only one entry 
    print(jsonData["orderData"]["txnType"]) 
    if jsonData["orderData"]["txnType"] == "SALE": 
     indSale+=44 
     indTotal+=1 
    else: 
     indRefund+=44 
else: 
    totalTransactions = len(jsonData["orderData"]) 
    while y < totalTransactions: 

     if jsonData["orderData"][y]["txnType"] == "SALE": 
      indSale+=44 
      indTotal+=1 
     else: 
      indRefund+=44 
     y+=1 

sales.append(indSale) 
refunds.append(indRefund) 
totalSales.append(indTotal) 
x+=1 
+0

它看起來像你正在運行這一個循環,你的結構是這樣orderDatas的列表。沒有更多的上下文,不能說太多。 –

+1

向我們展示產生錯誤的實際完整代碼。只是你顯示的一行沒有任何錯誤。 – jwodder

+0

只是添加了完整的代碼 – Sagar

回答

0

與您的數據,我可以打印出正確的值不會有問題,這意味着該數據是有效的。因此,請仔細檢查並驗證「jsonData」對象是否爲「sub-scriptable」並具有markdown中引用的「orderData」鍵。看起來你可能沒有得到預期的JSON響應。

也指爲子編寫腳本:In Python, what does it mean if an object is subscriptable or not?

希望它能幫助。

+0

只注意到你引用了[y],但在json中沒有jsonData [「orderData」]下的對象列表,所以這不起作用。 – Leo