【发布时间】:2015-12-30 00:11:41
【问题描述】:
给定一组页面,我正在制作 JSON。我想跳过任何没有标题的页面,最后一个元素后面不能有逗号,那是糟糕的 JSON。尝试了不同的变化,这里是一个例子:
---
---
[
{% for page in site.pages %}
{% unless page.title %}
{% continue %}
{% else %}
{
"title":"{{ page.title }}",
"content":"{{ page.content | strip_html | strip_newlines }}",
"href":"{{ page.url }}"
}
{% endunless %}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
生成的 JSON 文件的结尾如下所示:
{
"title":"Test Search",
"content":" ",
"href":"/search.html"
}
,
]
如何去掉结尾的逗号?提前谢谢你。
【问题讨论】:
标签: json for-loop jekyll liquid continue