首页 > 解决方案 > 在 django 模板中的字典中迭代字典

问题描述

{
    "0": {
        "id": "20",
        "user_id": "1865",
        "amount": "100",
        "currency_id": "153",
        "content_ids": "17408",
        "invoice_id": "72f5014243f8d",
        "order_number": "387049404M2007243",
        "gateway_response": "{\"TIMESTAMP\":\"2018-10-08T12:30:17Z\",\"CORRELATIONID\":\"72f5014243f8d\",\"ACK\":\"Success\",\"VERSION\":\"57.0\",\"BUILD\":\"46457558\",\"AMT\":\"100.00\",\"CURRENCYCODE\":\"USD\",\"AVSCODE\":\"Y\",\"CVV2MATCH\":\"S\",\"TRANSACTIONID\":\"387049404M2007243\"}",
        "created_date": "2018-10-24 12:42:55",
        "ordered_on": "Wed, Oct 24th '18",
        "currency_code": "USD",
        "currency_symbol": "$",
        "content_details": {
            "fid": "17408",
            "id": "4f6b08955b1fc1049cc51869f36b1853",
            "name": "Carol of the Bells SATB - arr. Jay Rouse",
            "story": null,
            "content_types_id": "6",
            "app_id": "5",
            "permalink": "carol-of-the-bells-satb-arr-jay-rouse"
        },
        "poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png",
        "seller_details": {
            "email": "afixi.gayadhar@gmail.com",
            "first_name": "Gayadhar khilar"
        },
        "price_details": {
            "price": "100",
            "price_id": "1",
            "code": "USD",
            "symbol": "$"
        }
    },

我想从上面的 json 响应中获取内容详细信息中的名称,但我无法迭代它。怎么做?

标签: python

解决方案


import json

strJson = '''
 <your JSON string>
'''

dictJson = json.loads(strJson)

# python 2.X
for key, value in dictJson.iteritems():
    print (value['content_details']['name'])

# python 3.X
for key, value in dictJson.items():
    print (value['content_details']['name'])

推荐阅读