英文:
Show specific JSON values in a nunjuck template
问题
以下是已翻译的内容:
尝试从JSON文件中筛选值并在nunjuck模板中显示。
这是JSON文件:
{"rows":
[
{"value":
{
"name":"'s Gravenwezel",
"zip":"2970"
}
},
{
"value":
{
"name":"'s Herenelderen",
"zip":"3700"
}
},
...
}
]
}
在nunjuck模板中添加**{{ cities }}**时,它会显示整个JSON文件,但如何只显示特定的值**name**和**zip**?
尝试这样做:
{{ cities.rows }}
{{ cities.rows[0] }}
{{ cities.rows[0].value[0].name }}
...
也尝试使用for循环:
{% for city in cities %}
{{ city[0] }}
{% endfor %}
以及许多其他组合。什么都不起作用!
希望这对你有所帮助。如果你需要进一步的翻译或有其他问题,请随时提问。
英文:
Trying filter values from a JSON-file and showing in an nunjuck-template.
This is the JSON-file:
{"rows":
[
{"value":
{
"name":"'s Gravenwezel",
"zip":"2970"
}
},
{
"value":
{
"name":"'s Herenelderen",
"zip":"3700"
}
},
...
}
]
}
When adding {{ cities }} in the nunjuck-template, it shows the whole JSON-file, but how can i show only the specific values name and zip?
Trying this:
{{ cities.rows }}
{{ cities.rows[0] }}
{{ cities.rows[0].value[0].name }}
...
Also trying with a for-loop:
{% for city in cities %}
{{ city[0] }}
{% endfor %}
And a lot of other combinations. Nothing works!
答案1
得分: 0
首先,我会修改for循环以使用cities.rows。然后,您可以通过value.X来访问键。所以,例如:
{% for city in cities.rows %}
{{ city.value.name }} 和 {{ city.value.zip }}<br/>
{% endfor %}
英文:
First, I'd modify the for loop to use cities.rows. Then you can address keys via value.X. So for example:
{% for city in cities.rows %}
{{ city.value.name }} and {{ city.value.zip }}<br/>
{% endfor %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论