【问题标题】:How can I parse a json string value of a json object in a liquid template?如何解析液体模板中 json 对象的 json 字符串值?
【发布时间】:2021-01-06 08:49:58
【问题描述】:

我对 Liquid 模板相当陌生,但我似乎没有找到将字符串值解析为 json 的方法。
免责声明:我在 VSCode 中使用 Shopify Liquid Preview extension

输入json文件

输入文件包含两个属性:StatusBody,我想找到一种方法来访问 Body 属性中的 ItemData.Name json 字段。

{
    "Status": true,
    "Body": "{\"ItemData\":{\"Amount\":10.0,\"Name\":\"MyTest\",\"ItemType\":1}}"
}

预期输出

<h1>MyTest</h1>
<p>Status: true<p>

当前尝试

尝试使用json_stringjson 过滤器,但乍一看似乎不起作用。

{% assign data = Body | json_string %}
{% assign data2 = Body | json %}
<h1>{{data.ItemData.Name}}</h1>
<h1>{{data2.ItemData.Name}}</h1>
<p>{{Status}}</p>

【问题讨论】:

  • 你能发布你想要实现的目标吗? json 是 statis 吗?

标签: liquid shopify-template


【解决方案1】:

我这样做是为了在我的一个项目中成功解析 JSON 字符串:

{% assign json_str = cart.attributes.urban-deli-pickup-spot %}
{% assign json_token = json_str | split: ',' %}

{% for token in json_token %}
    {% if forloop.index == 1 %}
        {% assign res = token | split: ':' %}
        {% assign resWithoutQuotes = res[1] | remove: '&quot;' %}
        {% capture pickup_spot %}
            Your amazing truck pickup spot is: {{ resWithoutQuotes }}
        {% endcapture %}
    {% endif %}
{% endfor %}

{{ pickup_spot }}

起点->

{"postal_address":"Evenemangsgatan 31","re​​quested_delivery_date":"2022-02-03","timewindow_start":"13:10","timewindow_end":"14:00"}强>

结果->

您的卡车取货地点是:Evenemangsgatan 31

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2012-09-28
    • 2018-04-13
    相关资源
    最近更新 更多