本文介绍了Symfony2 Serialze实体从输出中转义反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我的应用程序使用Symfony2.我正在使用序列化程序组件.
Hi I am using Symfony2 for my application. I am using the serializer component.
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$callback = function ($dateTime) {
return $dateTime instanceof \DateTime
? $dateTime->format(\DateTime::ISO8601)
: '';
};
$normalizer->setCallbacks(array('matchAStartTime' => $callback, 'matchBStartTime'=> $callback, 'matchDate'=> $callback));
$normalizer->setIgnoredAttributes(array('createdAt', 'updatedAt'));
$serializer = new Serializer(array($normalizer), array($encoder));
$json = $serializer->serialize($entity, 'json');
但是在输出中,我的响应是这样的:
but in the output i am having response like this:
\"id\":1,\"matchAStatus\":\"Live\"
我的问题是如何删除输出中的斜线?我知道在原始php中有转义反斜杠的选项,但是我可以在Symfony中使用什么?
my question is how can I remove that slash in output? I know in raw php there is option for escape backslash but what can I use in Symfony?
推荐答案
您可以使用JSON_UNESCAPED_SLASHES
常量(php> = 5.4.0).
You can use JSON_UNESCAPED_SLASHES
constant (php >= 5.4.0).
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
$encoder = new JsonEncoder(new JsonEncode(JSON_UNESCAPED_SLASHES), new JsonDecode(false))
这篇关于Symfony2 Serialze实体从输出中转义反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!