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

Python值存在时出现Python非类型错误,python,json,python-requests,Python,Json,Python Requests,因此,我的JSON数据如下所示。我的代码行:print(jsonData[“orderData”][“txnType”])打印出单词SALE,然后我得到一个错误,说TypeError:“NoneType”对象不可订阅 根据我的理解,如果值清楚地存在并打印出来,为什么会出现此错误 { 'orderData':{ 'date':'2017-08-29T12:55:19-07:00', 'receipt':'A2ZC5N96', 'promo':{

因此,我的JSON数据如下所示。我的代码行:
print(jsonData[“orderData”][“txnType”])
打印出单词SALE,然后我得到一个错误,说
TypeError:“NoneType”对象不可订阅
根据我的理解,如果值清楚地存在并打印出来,为什么会出现此错误

  {  
   '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
devKeys=[“KEUYS”]
apiKeys=[“API”]
销售额=[]
退款=[]
总销售额=[]
x=0
当x
使用您的数据,我可以毫无问题地打印出正确的值,这意味着数据是有效的。因此,请仔细检查并验证“jsonData”对象是否“可编写子脚本”,是否具有标记中引用的“orderData”键。您可能无法获得预期的JSON响应

另请参阅,以了解子脚本:


希望能有所帮助。

看起来您是在循环中运行此操作,并且您的结构是一个这样的OrderData列表。如果没有更多的上下文,就不能说太多。向我们展示产生错误的实际完整代码。仅显示的一行代码没有任何错误。只添加了完整的代码。显然,您向我们展示的JSON不是您想要的et,print
jsonData
print
语句之前,请同时发布完整的异常回溯。回溯中有大量信息。刚刚注意到您引用了[y],但json中jsonData[“orderData”]下没有对象列表,因此这不起作用。