我正试图从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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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": "john.doe@example.com",
"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,数量和完成日期。
答案 0 :(得分:0)
为了实现这一目标,您需要:
示例:
import json
json_obj = json.loads(json_string)
orders = json_obj["orders"]
completed = lambda order: order["status"] == "completed"
selected_orders = filter(completed, orders)