试图迭代通过列表寻找特定的字典键

问题描述:

我想从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

为了做到这一点,你需要:

  • 将您的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)