在 nunjuck 模板中显示特定的 JSON 值。

huangapple go评论66阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月3日 20:54:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定