2012-04-06 49 views
0

從JSON值在我的控制,我得到一個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值等。

回答

0

看起來c.order_history將是一個字典列表,只是抓住從這些字典的列表理解的product_info鍵,你會做到以下幾點:

[{'product_info': order['product_info']} for order in c.order_history] 
+0

謝謝。這肯定有幫助。 – 2012-04-06 22:55:02