Python 3尝试访问json数组不断收到关键错误

时间:2018-12-05 20:45:39

标签: python json

我正在使用python 3,正在尝试从json查询中访问某些信息。目前,我可以通过循环访问付款中的数据。我尝试执行相同的操作来访问order_status,但这是行不通的。

Json:

___order details___
{
   "message_type_name": "OrderPlaced",
   "order": {
       "adcode": "",
       "adcode_id": 0,      
       "billing_address": {
           "address_line_1": "123 east street",
           "address_line_2": "apt one"          
       },
       "campaign_code": "",
       "channel": "Online",      
       "customer": {
           "adcode": "",
           "adcode_id": 0,
           "affiliate_id": 0,
           "alternate_phone_number": ""          
       },    
       "device": "None",
       "discount_total": 0.0,
       "discounted_shipping_total": 0.0,      
       "items": [
           {
               "admin_comments": "",
               "cost": 90.0,
               "created_at": "2018-12-04T17:14:55.1128646-06:00"               
           }
       ],
       "manufacturer_invoice_amount": null,
       "manufacturer_invoice_number": "",
       "manufacturer_invoice_paid": false,      
       "order_status": {
           "color": "#A4E065",
           "created_at": null,
           "email_template_id": null,
           "id": 6,
           "is_cancelled": false,
           "is_declined": false,
           "is_fully_refunded": false,
           "is_open": true,
           "is_partially_refunded": false,
           "is_quote_status": false,
           "is_shipped": false,
           "name": "Awaiting Payment",
           "updated_at": "2015-10-12T22:07:47.487-05:00"
       },
       "order_status_id": 6,
       "order_status_last_changed_at": "2018-12-04T17:14:55.0503538-06:00",
       "order_type": "Order",      
       "payments": [
           {
               "amount": 100.00,
               "approved_at": "",
               "authorization_code": ""
               "
           }
       ],
       "ppc_keyword": "",
       "previous_order_status_id": 6,      
       "shipments": [],
       "shipping_address": {        
           "comments": "",
           "company": "",
           "country": "United States"          
       },
       "shipping_total": 0.0,
       "source": "Google [organic]"      
   },
   "store_id": 1
}

我有一个名为data的变量:data = json.loads(self.rfile.read(length).decode('utf-8'))

我还有另一个变量order_payments = data ['payments'] 我可以循环浏览并访问is_declined

我想要一个名为order_status = data ['order_status']的变量,然后循环访问该变量以访问名称。我在order_status = data ['order_status']上遇到关键错误,但我不知道为什么。

0 个答案:

没有答案