在我的控制器中,我得到一个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)
将其转换为dict,然后尝试提取每个键,以便我可以在其中获取后续的键/值,如:
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
值等吗?
答案 0 :(得分:0)
看起来c.order_history
将是一个字典列表,只需从列表解析中获取每个字典中的product_info
键,您将执行以下操作:
[{'product_info': order['product_info']} for order in c.order_history]