如何使用 Jinja 访问嵌套的 JSON?

How to access nested JSON with Jinja?

我正在学习 Jinja 以构建一个简单的网站。如何访问 Jinja 语法中嵌套 JSON 的值?

示例: 如何仅访问下面 JSON 中的 "Education" 键来填充 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"}
            ]
        }
    ]
}

HTML 片段 w/ Jinja

{% 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 d in data %}
    {% for e in d[Education] %}
    <code shown above>
    {% end for %}
{% end for %}

你想要的是这样的:

{% 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 %}

需要在 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>