繁体   English   中英

在django模板中的字典中迭代字典

[英]Iterate a dictionary in dictionary in django template

{
    "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 响应中获取内容详细信息中的名称,但我无法对其进行迭代。 怎么做?

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'])
json.load(open("/tmp/j.json"))["0"]["content_details"]["name"]

结果:

'Carol of the Bells SATB - arr. Jay Rouse'

如果dict是上述构造的名称,则dict['0']['contend_details']['name']是您要查找的属性(“Carol of the Bells SATB - arr. Jay Rouse”在这种情况下)

import json
from pprint import pprint

with open('data.json') as f:
    data = json.load(f)

pprint(data["0"]["content_details"]["name"])

你可以试试这个:

 all_content_details = [your_json[x]['content_details'] for x in your_json.keys()] print(all_content_details)

这应该允许您遍历 JSON 的所有内容详细信息

我猜你正在寻找这样的东西?

j = json.load(json_string)
for x in j:
    print(j[x]['content_details']['name'])
for item, value in d['0']['content_details'].items():
    if item == 'name':
        break
    print(value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM