[英]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.