使用液体和变量从JSON对象获取字段

发布于 2025-01-31 07:39:08 字数 734 浏览 2 评论 0原文

我正在尝试使用液体模板创建推送通知,但不知道如何从JSON中提取特定字段。

我有一个创建液体对象的JSON {%分配对象=响应|替换:'=>',':'| JSON_PARSE%}

JSON看起来像这样:

[
    {
        "en": "Hello!:",
        "sv": "Hej!"
    }
]

我可以使用:{{object [0] .en}}会给我“ Hello hello”

,但是我有语言存储在称为语言的变量中,我不想对语言进行编码。

取而代之的是,我想使用变量语言获取正确的值,但我无法使其工作。我已经尝试了以下内容:

  {{object[0]['${language}']}}
  {{object[0][${language}]}}
  {{object[0].${language}}}
  {{object[0].'${language}'}}

有什么想法如何使用我的Lakenagent变量在JSON对象中获取正确的值?


edit :我想知道我需要在双卷发括号前添加空白空间,然后它的工作原理 {{object [2] [$ {MOST_RECENT_LOCALE}]}}}

I'm trying to create a push notification using a Liquid template but don't know how to extract a specific field from the json.

I have a json which I create an Liquid object
{% assign object = response | replace: '=>', ':' | json_parse %}

The json looks like this:

[
    {
        "en": "Hello!:",
        "sv": "Hej!"
    }
]

I can access the field using: {{object[0].en}} which will give me "Hello"

However, I have the language stored in a variable called language and I don't want to hardcode the language.

Instead, I would like to use the variable language to fetch the correct value but I can not get it to work. I have tried the below:

  {{object[0]['${language}']}}
  {{object[0][${language}]}}
  {{object[0].${language}}}
  {{object[0].'${language}'}}

Any ideas how I can fetch the correct value based in the json object using my language variable?


edit: I figure out that I needed to add a blankspace after an before the double curly brackets, then it works perfect e.g
{{ object[2][${most_recent_locale}] }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文