You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法访问Shopify原生元字段中的JSON数组,求技术解决方案

Shopify 元字段数组遍历解决方案

先排查元字段类型

确保你的source_snippet元字段在Shopify后台(或新版Metafields Guru)中设置的类型是List of objects,而非普通文本类型。如果之前用旧版工具存的是JSON字符串,需要调整类型,或者手动解析。

对应代码实现

情况1:元字段类型是List of objects

直接通过.value获取数组后遍历:

{% assign sources = article.metafields.custom.source_snippet.value %}
{% for source in sources %}
  <div class="source-item">
    <h4>{{ source.title }}</h4>
    <p>发布方:{{ source.pub }}</p>
    <a href="{{ source.url }}" rel="noopener noreferrer">查看原文</a>
  </div>
{% endfor %}

情况2:元字段存的是JSON字符串(文本类型)

需要先用json过滤器解析成数组再遍历:

{% assign raw_sources = article.metafields.custom.source_snippet.value %}
{% assign sources = raw_sources | json %}
{% for source in sources %}
  <div class="source-item">
    <span>ID:{{ source.id }}</span>
    <h4>{{ source.title }}</h4>
    <p>发布方:{{ source.pub }}</p>
    <a href="{{ source.url }}" rel="noopener noreferrer">查看原文</a>
  </div>
{% endfor %}

调试技巧

如果还是无效,先输出元字段的原始结构确认问题:

{{ article.metafields.custom.source_snippet | inspect }}

根据输出结果判断是类型错误、未解析JSON,还是其他格式问题。

内容的提问来源于stack exchange,提问作者Tajs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 13:16:16