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