在 Twig 中解码 JSON
是否可以在树枝中解码JSON?谷歌搜索似乎没有产生任何关于此的结果。在Twig中解码JSON没有意义吗?
我正在尝试访问Symfony2的实体字段类型(实体字段类型)上的2个实体属性。
在遇到之前的2个SO问题(Symfony2实体字段类型替代“属性”或“__toString()”?和Symfony 2创建一个具有2个属性的实体表单字段)之后,建议向实体添加一个额外的方法来检索自定义字符串而不是实体属性,我想到(并且确实)返回一个表示对象实例的JSON字符串。
实体类中的某个位置:
/**
* Return a JSON string representing this class.
*/
public function getJson()
{
return json_encode(get_object_vars($this));
}
在形式上(类似于):
$builder->add('categories', 'entity', array (
...
'property' => 'json',
...
));
之后,我希望在Twig中...json_decode
{% for category in form.categories %}
{# json_decode() part is imaginary #}
{% set obj = category.vars.label|json_decode() %}
{% endfor %}