Symfony 4:在 JSON 响应中未加斜线

Symfony 4 : slashes unscaped on JSON response

大家早上好,

我的 symfony 4 Api 有问题。

我应该 return 一个 json 响应,但是序列化程序 return 一个带斜杠的字符串。我不知道如何逃脱它。

吼我的控制器:


use App\Entity\Category;
use App\Form\CategoryType;
use App\Repository\CategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface as SerializerInterfaceAlias;
use FOS\RestBundle\Controller\Annotations as Rest;

/**
 * Category Controller
 * @Route("/api", name="api_")
 */
class CategoryController extends AbstractController
{
    /**
     * @Rest\Get("/categories")
     * @param CategoryRepository $categoryRepository
     * @param SerializerInterfaceAlias $serializer
     */
    public function index(CategoryRepository $categoryRepository, SerializerInterfaceAlias $serializer)
    {
        $jsonContent = $serializer->serialize($categoryRepository->findall(), 'json', ['json_encode_options' => JSON_UNESCAPED_SLASHES]);
        return $jsonContent;
    }
[....]
}

我的 return 看起来像:

"[{\"id\":3,\"name\":\"toto\",\"logo\":\"tata\",\"description\":\"lolo\",\"dateCreated\":\"2019-05-09T10:30:39+00:00\"},{\"id\":4,\"name\":\"tata\",\"logo\":\"titi\",\"description\":\"tutu\",\"dateCreated\":\"2019-05-09T10:30:49+00:00\"}]"

有关我使用 PHP 7.1 & Symfony 4.2.

的信息

所以我想要一个合适的 json 格式...没有这个斜杠 :(

你有什么建议吗? :)

提前致谢!

我终于解决了我的问题#RubberDuckDebugging

我这里不需要使用序列化器。

我只需要 return :

return $this->json($categoryRepository->findall());

就这么简单。对不起:)