2015-10-06 90 views
-3

我想從JSON中拉出交易項目列表。我知道我尋找的關鍵是什麼,但是需要遍歷列表尋找關鍵字,然後從那裏取出特定的信息。生JSON的樣品是:試圖迭代通過列表尋找特定的字典鍵

{ 
    "orders": [ 
    { 
     "id": 645, 
     "order_number": 645, 
     "created_at": "2015-01-26T20:00:21Z", 
     "updated_at": "2015-01-26T20:00:21Z", 
     "completed_at": "2015-01-26T20:00:21Z", 
     "status": "processing", 
     "currency": "USD", 
     "total": "79.87", 
     "subtotal": "63.97", 
     "total_line_items_quantity": 3, 
     "total_tax": "5.90", 
     "total_shipping": "10.00", 
     "cart_tax": "5.40", 
     "shipping_tax": "0.50", 
     "total_discount": "0.00", 
     "shipping_methods": "Flat Rate", 
     "payment_details": { 
     "method_id": "bacs", 
     "method_title": "Direct Bank Transfer", 
     "paid": true 
     }, 
     "billing_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US", 
     "email": "[email protected]", 
     "phone": "(555) 555-5555" 
     }, 
     "shipping_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US" 
     }, 
     "note": "", 
     "customer_ip": "127.0.0.1", 
     "customer_user_agent": "WordPress/4.1; http://example.com", 
     "customer_id": 2, 
     "view_order_url": "https://example.com/my-account/view-order/645", 
     "line_items": [ 
     { 
      "id": 504, 
      "subtotal": "43.98", 
      "subtotal_tax": "4.40", 
      "total": "43.98", 
      "total_tax": "4.40", 
      "price": "21.99", 
      "quantity": 2, 
      "tax_class": "reduced-rate", 
      "name": "Premium Quality", 
      "product_id": 546, 
      "sku": "", 
      "meta": [] 
     }, 
     { 
      "id": 505, 
      "subtotal": "19.99", 
      "subtotal_tax": "1.00", 
      "total": "19.99", 
      "total_tax": "1.00", 
      "price": "19.99", 
      "quantity": 1, 
      "tax_class": null, 
      "name": "Ship Your Idea", 
      "product_id": 613, 
      "sku": "", 
      "meta": [ 
      { 
       "key": "pa_color", 
       "label": "Color", 
       "value": "Black" 
      } 
      ] 
     } 
     ], 
     "shipping_lines": [ 
     { 
      "id": 506, 
      "method_id": "flat_rate", 
      "method_title": "Flat Rate", 
      "total": "10.00" 
     } 
     ], 
     "tax_lines": [ 
     { 
      "id": 507, 
      "rate_id": "5", 
      "code": "US-CA-TAX-1", 
      "title": "Tax", 
      "total": "4.40", 
      "compound": false 
     }, 
     { 
      "id": 508, 
      "rate_id": "4", 
      "code": "US-STANDARD-1", 
      "title": "Standard", 
      "total": "1.50", 
      "compound": false 
     } 
     ], 
     "fee_lines": [], 
     "coupon_lines": [], 
     "customer": { 
     "id": 2, 
     "created_at": "2014-11-19T18:34:19Z", 
     "email": "[email protected]", 
     "first_name": "", 
     "last_name": "", 
     "username": "john.doe", 
     "last_order_id": "645", 
     "last_order_date": "2015-01-26T20:00:21Z", 
     "orders_count": 2, 
     "total_spent": "19.00", 
     "avatar_url": "https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=96", 
     "billing_address": { 
      "first_name": "John", 
      "last_name": "Doe", 
      "company": "", 
      "address_1": "969 Market", 
      "address_2": "", 
      "city": "San Francisco", 
      "state": "CA", 
      "postcode": "94103", 
      "country": "US", 
      "email": "[email protected]", 
      "phone": "(555) 555-5555" 
     }, 
     "shipping_address": { 
      "first_name": "John", 
      "last_name": "Doe", 
      "company": "", 
      "address_1": "969 Market", 
      "address_2": "", 
      "city": "San Francisco", 
      "state": "CA", 
      "postcode": "94103", 
      "country": "US" 
     } 
     } 
    }, 
    { 
     "id": 644, 
     "order_number": 644, 
     "created_at": "2015-01-26T19:33:42Z", 
     "updated_at": "2015-01-26T19:33:42Z", 
     "completed_at": "2015-01-26T19:33:42Z", 
     "status": "on-hold", 
     "currency": "USD", 
     "total": "44.14", 
     "subtotal": "30.99", 
     "total_line_items_quantity": 2, 
     "total_tax": "3.15", 
     "total_shipping": "10.00", 
     "cart_tax": "2.65", 
     "shipping_tax": "0.50", 
     "total_discount": "0.00", 
     "shipping_methods": "Flat Rate", 
     "payment_details": { 
     "method_id": "bacs", 
     "method_title": "Direct Bank Transfer", 
     "paid": false 
     }, 
     "billing_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US", 
     "email": "[email protected]", 
     "phone": "(555) 555-5555" 
     }, 
     "shipping_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US" 
     }, 
     "note": "", 
     "customer_ip": "127.0.0.1", 
     "customer_user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36", 
     "customer_id": 2, 
     "view_order_url": "https://example.com/my-account/view-order/644", 
     "line_items": [ 
     { 
      "id": 499, 
      "subtotal": "21.99", 
      "subtotal_tax": "2.20", 
      "total": "21.99", 
      "total_tax": "2.20", 
      "price": "21.99", 
      "quantity": 1, 
      "tax_class": "reduced-rate", 
      "name": "Premium Quality", 
      "product_id": 546, 
      "sku": "", 
      "meta": [] 
     }, 
     { 
      "id": 500, 
      "subtotal": "9.00", 
      "subtotal_tax": "0.45", 
      "total": "9.00", 
      "total_tax": "0.45", 
      "price": "9.00", 
      "quantity": 1, 
      "tax_class": null, 
      "name": "Woo Album #4", 
      "product_id": 96, 
      "sku": "", 
      "meta": [] 
     } 
     ], 
     "shipping_lines": [ 
     { 
      "id": 501, 
      "method_id": "flat_rate", 
      "method_title": "Flat Rate", 
      "total": "10.00" 
     } 
     ], 
     "tax_lines": [ 
     { 
      "id": 502, 
      "rate_id": "5", 
      "code": "US-CA-TAX-1", 
      "title": "Tax", 
      "total": "4.40", 
      "compound": false 
     }, 
     { 
      "id": 503, 
      "rate_id": "4", 
      "code": "US-STANDARD-1", 
      "title": "Standard", 
      "total": "1.50", 
      "compound": false 
     } 
     ], 
     "fee_lines": [], 
     "coupon_lines": [], 
     "customer": { 
     "id": 2, 
     "created_at": "2014-11-19T18:34:19Z", 
     "email": "[email protected]", 
     "first_name": "", 
     "last_name": "", 
     "username": "john.doe", 
     "last_order_id": "645", 
     "last_order_date": "2015-01-26T20:00:21Z", 
     "orders_count": 2, 
     "total_spent": "19.00", 
     "avatar_url": "https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=96", 
     "billing_address": { 
      "first_name": "John", 
      "last_name": "Doe", 
      "company": "", 
      "address_1": "969 Market", 
      "address_2": "", 
      "city": "San Francisco", 
      "state": "CA", 
      "postcode": "94103", 
      "country": "US", 
      "email": "[email protected]", 
      "phone": "(555) 555-5555" 
     }, 
     "shipping_address": { 
      "first_name": "John", 
      "last_name": "Doe", 
      "company": "", 
      "address_1": "969 Market", 
      "address_2": "", 
      "city": "San Francisco", 
      "state": "CA", 
      "postcode": "94103", 
      "country": "US" 
     } 
     } 
    } 
    ] 
} 
Available Filters 

Filter Type Description 
status string Orders by status. eg: processing or cancelled 
Update An Order 
This API lets you make changes to an order. 

HTTP Request 

PUT /wc-api/v3/orders/<id> 
data = { 
    "order": { 
     "status": "completed" 
    } 
} 

print(wcapi.put("orders/645", data).json()) 
JSON response example: 
{ 
    "order": { 
    "id": 645, 
    "order_number": 645, 
    "created_at": "2015-01-26T20:00:21Z", 
    "updated_at": "2015-01-26T20:00:21Z", 
    "completed_at": "2015-01-26T20:00:21Z", 
    "status": "completed", 
    "currency": "USD", 
    "total": "79.87", 
    "subtotal": "63.97", 
    "total_line_items_quantity": 3, 
    "total_tax": "5.90", 
    "total_shipping": "10.00", 
    "cart_tax": "5.40", 
    "shipping_tax": "0.50", 
    "total_discount": "0.00", 
    "shipping_methods": "Flat Rate", 
    "payment_details": { 
     "method_id": "bacs", 
     "method_title": "Direct Bank Transfer", 
     "paid": true 
    }, 
    "billing_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US", 
     "email": "[email protected]", 
     "phone": "(555) 555-5555" 
    }, 
    "shipping_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US" 
    }, 
    "note": "", 
    "customer_ip": "127.0.0.1", 
    "customer_user_agent": "WordPress/4.1; http://example.com", 
    "customer_id": 2, 
    "view_order_url": "https://example.com/my-account/view-order/645", 
    "line_items": [ 
     { 
     "id": 504, 
     "subtotal": "43.98", 
     "subtotal_tax": "4.40", 
     "total": "43.98", 
     "total_tax": "4.40", 
     "price": "21.99", 
     "quantity": 2, 
     "tax_class": "reduced-rate", 
     "name": "Premium Quality", 
     "product_id": 546, 
     "sku": "", 
     "meta": [] 
     }, 
     { 
     "id": 505, 
     "subtotal": "19.99", 
     "subtotal_tax": "1.00", 
     "total": "19.99", 
     "total_tax": "1.00", 
     "price": "19.99", 
     "quantity": 1, 
     "tax_class": null, 
     "name": "Ship Your Idea", 
     "product_id": 613, 
     "sku": "", 
     "meta": [ 
      { 
      "key": "pa_color", 
      "label": "Color", 
      "value": "Black" 
      } 
     ] 
     } 
    ], 
    "shipping_lines": [ 
     { 
     "id": 506, 
     "method_id": "flat_rate", 
     "method_title": "Flat Rate", 
     "total": "10.00" 
     } 
    ], 
    "tax_lines": [ 
     { 
     "id": 507, 
     "rate_id": "5", 
     "code": "US-CA-TAX-1", 
     "title": "Tax", 
     "total": "4.40", 
     "compound": false 
     }, 
     { 
     "id": 508, 
     "rate_id": "4", 
     "code": "US-STANDARD-1", 
     "title": "Standard", 
     "total": "1.50", 
     "compound": false 
     } 
    ], 
    "fee_lines": [], 
    "coupon_lines": [], 
    "customer": { 
     "id": 2, 
     "created_at": "2014-11-19T18:34:19Z", 
     "email": "[email protected]", 
     "first_name": "", 
     "last_name": "", 
     "username": "john.doe", 
     "last_order_id": "645", 
     "last_order_date": "2015-01-26T20:00:21Z", 
     "orders_count": 2, 
     "total_spent": "19.00", 
     "avatar_url": "https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=96", 
     "billing_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US", 
     "email": "[email protected]", 
     "phone": "(555) 555-5555" 
     }, 
     "shipping_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "company": "", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US" 
     } 
    } 
    } 
} 

我期待然後把它變成一個事務性數據框中將列出SKU,數量和日期完成。

+2

你能告訴你的代碼,這個好嗎? – idjaw

+0

你知道如何將該文本轉換爲python對象嗎? – Pynchia

回答

0

爲了做到這一點,你需要:

  • 將您的JSON字符串轉換成JSON對象(使用json模塊);
  • 創建一個按照您的標準過濾訂單的函數;
  • 運行過濾功能,以提取匹配的訂單

例子:

import json 

json_obj = json.loads(json_string) 
orders = json_obj["orders"] 
completed = lambda order: order["status"] == "completed" 
selected_orders = filter(completed, orders)