0

在我的控制器中,我得到一个 json 字符串(调用c.order_history它看起来像:

[
{
    "status": [
        {
            "status": "created",
            "timestamp": "2012-04-06 00:14:10"
        },
        {
            "status": "authed",
            "timestamp": "2012-04-06 00:14:17"
        }
    ],
    "product_info": [
        {
            "id": 3,
            "quantity": 1,
            "created": "2012-04-06 00:14:10",
            "image_id": 13341
        },
        {
            "id": 2,
            "quantity": 1,
            "created": "2012-04-06 00:14:10",
            "image_id": 13323
        },
        {
            "id": 1,
            "quantity": 1,
            "created": "2012-04-06 00:14:10",
            "image_id": 13322
        }
    ],
    "shipping_charge": "0.00",
    "order_number": "0723094747433",
    "shipping_address": {
        "country_code": null,
        "extended_address": "Unit Z",
        "locality": "Las Vagas",
        "company": null,
        "phone": null,
        "postal_code": "31415",
        "full_name": "Boris Karloff",
        "nickname": null,
        "region": "NV",
        "street_address": "123 Random Way"
    },
    "subtotal": "59.00"
}

]

我通过它json.loads(order_history)把它变成一个字典,然后尝试提取每个键,这样我就可以得到它们中的后续键/值,例如:

c.product_info = [{'product_info' : product_info} for product_info in c.order_history]

它输出整个 json 字符串,但它product_info现在刚刚命名。有人可以引导我朝着正确的方向前进,了解我如何访问说、timestamp价值product_info[0]['image_id']shipping_address价值等吗?

4

1 回答 1

0

看起来c.order_history将是一个字典列表,只需product_info从列表理解中的每个字典中获取键,您将执行以下操作:

[{'product_info': order['product_info']} for order in c.order_history]
于 2012-04-06T20:17:29.943 回答