这是情况:我在一个基于symfony3的rest api上工作,它使用FOSRestBundle和symfony序列化程序组件,因此方法返回数组,FOSRest处理编码和响应。问题是序列化程序使用带有默认设置的json_encode和api为某些符号返回'\ u00c9S'之类的数据。所以我需要以某种方式将'JSON_UNESCAPED_UNICODE'传递给json_encode()。有没有适当的方法来达到这个目标?方法示例:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\Controller\Annotations as Rest;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ExampleController
* @package AppBundle\Controller
*/
class ExampleController extends Controller
{
/**
* @Rest\Get("/get-some-data")
* @param Request $request
* @return array
*/
public function getSomeDataAction(Request $request)
{
$someData = [
'prop1' => 'Value',
'prop2' => 'Value',
'prop3' => 'Value',
'prop4' => 'Value',
];
return $someData;
}
所以,当我要求'/ get-some-data'时,它会返回给我:
{"prop1":"Value with \/","prop2":"Value with \u00c9"}
,但我需要它返回:
{"prop1":"Value with /","prop2":"Value with É"}
你可以使用编码器,如here和documentation
<?php
$encoder = new JsonEncoder(new JsonEncode(JSON_UNESCAPED_UNICODE), new JsonDecode(false));
$normalizer = new ObjectNormalizer();
$serializer = new Serializer(array($normalizer), array($encoder));
编辑:
在此示例中,我使用Response对象。请注意,Action Controller必须返回Response对象。
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\Controller\Annotations as Rest;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ExampleController
* @package AppBundle\Controller
*/
class ExampleController extends Controller
{
/**
* @Rest\Get("/get-some-data")
* @param Request $request
* @return array
*/
public function getSomeDataAction(Request $request)
{
$someData = [
'prop1' => 'Value',
'prop2' => 'Value',
'prop3' => 'Value',
'prop4' => 'Value',
];
$response = new Response($someData);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
我使用Symfony 3和“Doctrine JSON ODM Bundle”将我的数据存储为JSON文档。我有同样的问题。所有包含自动转义的unicode字符的数据都不是我想要的。
经过一些实验,我终于设法将JSON_UNESCAPED_UNICODE
选项传递给json_encode()
。以下是我的解决方案:
# config/services.yml
serializer.encode.json.unescaped:
class: Symfony\Component\Serializer\Encoder\JsonEncode
arguments:
- !php/const JSON_UNESCAPED_UNICODE
serializer.encoder.json:
class: Symfony\Component\Serializer\Encoder\JsonEncoder
arguments:
- '@serializer.encode.json.unescaped'
当设置json类型的doctrine for the column时,使用PHP的json_encode()和json_decode()函数来存储和检索数据库中的数据。
将PHP的数组转换为JSON字符串的json_encode()函数具有第二个参数,用于分配用于设置某些转换规则的选项集。其中一个是JSON_UNESCAPED_UNICODE,禁用编码多字节符号为\ uXXXX
对于doctrine,我们可以为json encode()启用此选项。要利用这一点,您必须执行两个步骤。
1.创建类App \ Doctrine \ Types \ JsonType
// App\Doctrine\Types\JsonType.php
<?php
namespace App\Doctrine\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class JsonType extends \Doctrine\DBAL\Types\JsonType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
$encoded = json_encode($value, JSON_UNESCAPED_UNICODE);
if (json_last_error() !== JSON_ERROR_NONE) {
throw ConversionException::conversionFailedSerialization($value, 'json', json_last_error_msg());
}
return $encoded;
}
}
2.覆盖与我们的类对应的DBAL JSON类型的配置
# config/packages/doctrine.yaml
doctrine:
dbal:
types:
json: App\Doctrine\Types\JsonType