简体   繁体   中英

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": "$"
        }
    },

I want to get name in content details from the above json response, but I am unable to iterate it. How to do so?

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"])

You could try this:

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

This should allow you to iterate though all the content details of your JSON

I guess you're looking for something like this ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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