我正在學習 Jinja 以建立一個簡單的網站。如何在 Jinja 語法中訪問嵌套 JSON 的值?
示例:如何僅訪問"Education"下面 JSON 中的密鑰來填充 HTML 片段?
JSON 片段
{
"Education": [
{
"SchoolType": "University",
"School": "State University",
"SchoolURL": "https://admission.edu",
"StartMonth": 9,
"StartYear": 2019,
"EndMonth": 5,
"EndYear": 2022,
"City": "Test",
"State": "WA",
"Attainment": "Bachelor's Degree",
"Accolades": [
{
"AccoladeType": "Major",
"Accolade": "Test Communications"
}
]
},
{
"SchoolType": "Junior College",
"School": "Pierce College",
"SchoolURL": "https://www.pierce.edu/",
"StartMonth": 9,
"StartYear": 2017,
"EndMonth": 6,
"EndYear": 2019,
"City": "Lakewood",
"State": "WA",
"Attainment": "Diploma",
"Accolades": [
]
}
],
"Employers": [
{
"EmploymentType": "Food Service",
"Employer": "Test Brew",
"EmployerURL": "",
"StartMonth": 9,
"StartYear": 2020,
"EndMonth": null,
"EndYear": null,
"City": "Olympia",
"State": "WA",
"PositionsHeld": [
{"Position": "Barista"}
]
},
{
"EmploymentType": "Food Service",
"Employer": "The Steakhouse",
"EmployerURL": "https://www.steakmill.com/",
"StartMonth": 7,
"StartYear": 2019,
"EndMonth": 1,
"EndYear": 2020,
"City": "Milton",
"State": "WA",
"PositionsHeld": [
{"Position": "Busser"},
{"Position": "Dishwasher"}
]
}
]
}
帶有 Jinja 的 HTML 片段
{% for d in data %}
<section class="blocks">
<div class="date">
<span>{{ d.StartMonth }}/{{ d.StartYear }}</span>
<span>{{ d.EndMonth }}/{{ d.EndYear }}</span>
</div>
<div class="decorator"></div>
<div class="details">
<header>
<h3>{{ d.Attainment }}</h3>
<span class="place"><a href={{ d.SchoolURL }}>{{ d.School }}</a></span>
<span class="location">{{ d.City }}, {{ d.State }}</span>
</header>
</div>
</section>
{% endfor %}
試過(有各種錯誤)
- 嵌套
for
{% for d in data %}
{% for e in d[Education] %}
<code shown above>
{% end for %}
{% end for %}
d.StartMonthd[Education].StartMonthd[Education][StartMonth]
uj5u.com熱心網友回復:
你想要的是這樣的:
{% for key, value in data.items() if key == 'Education' %}
{% for v in value %}
<section class="blocks">
<div class="date">
<span>{{ v.StartMonth }}/{{ v.StartYear }}</span>
<span>{{ v.EndMonth }}/{{ v.EndYear }}</span>
</div>
<div class="decorator"></div>
<div class="details">
<header>
<h3>{{ v.Attainment }}</h3>
<span class="place"><a href={{ v.SchoolURL }}>{{ v.School }}</a></span>
<span class="location">{{ v.City }}, {{ v.State }}</span>
</header>
</div>
</section>
{% endfor %}
{% endfor %}
通過使用items(),它將允許您遍歷鍵值對(“教育”,值陣列)
您還可以使用點表示法來拉取鍵,如下所示:
{% for d in data.Education %}
<section class="blocks">
<div class="date">
<span>{{ d.StartMonth }}/{{ d.StartYear }}</span>
<span>{{ d.EndMonth }}/{{ d.EndYear }}</span>
</div>
<div class="decorator"></div>
<div class="details">
<header>
<h3>{{ d.Attainment }}</h3>
<span class="place"><a href={{ d.SchoolURL }}>{{ d.School }}</a></span>
<span class="location">{{ d.City }}, {{ d.State }}</span>
</header>
</div>
</section>
{% endfor %}
uj5u.com熱心網友回復:
需要在data物件之后使用點符號來鉆入嵌套的 JSON。
這有效...
{% for e in data.Education %}
<section class="blocks">
<div class="date">
<span>{{ e.StartMonth }}/{{ e.StartYear }}</span>
<span>{{ e.EndMonth }}/{{ e.EndYear }}</span>
</div>
<div class="decorator"></div>
<div class="details">
<header>
<h3>{{ e.Attainment }}</h3>
<span class="place"><a href={{ e.SchoolURL }}>{{ e.School }}</a></span>
<span class="location">{{ e.City }}, {{ e.State }}</span>
</header>
</div>
</section>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468895.html
