使用Twig在JSON对象数组中按ID查找对象 [英] Find object by id in an array of JSON objects with Twig
问题描述
我正在传递一个数组,不幸的是我无法重组:
I am being passed an array that unfortunately I cannot restructure:
"options": [
{"name":"namea","text":"valuea"},
{"name":"nameb","text":"valueb"},
{"name":"namec","text":"valuec"},
{"name":"named","text":"valued"}
]
我需要能够找到名称等于namea,nameb,namec等的对象,然后生成适当的文本.我尝试了以下方法以及其他几种方法,但无法使其正常工作:
I need to be able to find the object with the name equal to namea, nameb, namec, etc. and then produce the appropriate text. I tried the following and few other variations of that but could not get it to work:
{% for item in event.options %}
{% if item.name == "nameb" %}
{{ item.text|capitalize }}
{% endif %}
{% endfor %}
在此先感谢您的帮助.
我基本上只能访问一个文本框来输入HTML和Twig.我无权访问框架,自定义扩展等.
I basically only have access to a text box to input HTML and Twig. I do not have access to framework, custom extensions, etc.
具有完全格式的JSON:
JSON with exact formatting:
{
"source": "TEST.COM",
"trigger": "test",
"options": [{
"name":"test_date",
"value":"31-05-2017"
},
{
"name":"test_number",
"value":"9081003"
},
{
"name":"test_test",
"value":"9asd003"
},
{
"name":"Name",
"value":"Todd"
},
{
"name":"test_other",
"value":"kslkjsfd"
},
{
"name":"test_help",
"value":"908sdf3"
}]
}
推荐答案
这两种方法都非常hacky(我认为评论中提到的javascript方法仍然不太hacky).
This is going to be very hacky either way (I feel the javascript way as mentioned in the comments would still be less hacky).
尽管它是多行的,但我们可以编写类似东西的基本解析器(这绝不是防弹的,如果内容带有",或者结构发生更改或说实话,则不会失败):
However as its multilined we could write a basic parser like thing (this is by no means bullet proof and fails if the content has a ", or the structure changes or whatever changes to be honest):
{# create an array of lines #}
{% set event = event|split('\n') %}
{# an array to hold our options #}
{% set options = [] %}
{# using a bool to track if we are inside the options part #}
{% set inOptions = false %}
{# this holds the name of the last name value we have passed #}
{% set currentKey = '' %}
{# go through each line #}
{% for line in event %}
{# if we are inside the options tag do our magic #}
{% if inOptions %}
{# check if the line starts with "name": and track the current key name #}
{# or check if it starts with "value": and set the current key to that value #}
{% if line matches '/"name":/' %}
{# split line on the double quotes and get the last bit #}
{% set currentKey = line|split('"') %}
{# cant get the array index piped in one go idk.. #}
{% set currentKey = currentKey[3] %}
{% elseif line matches '/"value":/i' %}
{% set currentValue = line|split('"') %}
{% set options = options|merge({(currentKey): currentValue[3]}) %}
{% elseif line matches '/\s*}]/' %}
{% set inOptions = false %}
{% endif %}
{% endif %}
{# check for the options sectioning start #}
{% if line matches '/^"options/' %}
{% set inOptions = true %}
{% endif %}
{% endfor %}
{{ dump(options) }}
这将返回到我的转储中(您没有,所以看不到):
This returns in my dump (which you didn't have so you cant see):
array:6 [▼
"test_date" => "31-05-2017"
"test_number" => "9081003"
"test_test" => "9asd003"
"Name" => "Todd"
"test_other" => "kslkjsfd"
"test_help" => "908sdf3"
]
作为一个实际的数组,您可以在其中调用options.test_date
,但所有这些都是超级hacky,它仍然可以改进,但是并没有为此而生,语法很难处理.
As an actual array where you can then call options.test_date
but all this is super hacky, it still can be improved but twig is not made for it and the syntax gets so clunky its hard to maintain.
这篇关于使用Twig在JSON对象数组中按ID查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!