在 Liquid 中调用富文本类型的元字段前端输出的是JSON 数据,应该怎么解析?
{{ product.metafields.custom.product_contenu_text }}
前端输出:
{"type":"root","children":[{"type":"heading","level":3,"children":[{"type":"text","value":"La cryolipolyse, comment ça marche ?"}]},{"type":"paragraph","children":[{"type":"text","value":"La cryolipolyse est une méthode minceur qui fonctionne en refroidissant la graisse sous la peau jusqu'à la geler."}]}]}
尝试:
{% assign content = product.metafields.custom.product_contenu_text %}
{% if content.type == "root" %}
{% for child in content.children %}
{% if child.type == "heading" %}
{% assign heading_level = child.level | default: 3 %}
<h{{ heading_level }}>{{ child.children[0].value }}</h{{ heading_level }}>
{% elsif child.type == "paragraph" %}
<p>{{ child.children[0].value }}</p>
{% endfor %}
{% endif %}
但是前端输出的是空白的,content.type == "root" 没有进入判断。