当值存在Python时出现Python非类型错误



所以我的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

使用您的数据,我可以毫无问题地打印出正确的值,这意味着数据是有效的。因此,请仔细检查并验证"jsonData"对象是否是"可编写子脚本的",并且具有markdown中引用的"orderData"键。看起来您可能没有得到预期的JSON响应。

也可以参考子脚本:在Python中,如果一个对象是可下标的或不可下标的,这意味着什么?

希望能有所帮助。

最新更新