FOSRestBundle &JMSSerializer:错误的 Json 响应 [英] FOSRestBundle & JMSSerializer : Wrong Json Response

查看:31
本文介绍了FOSRestBundle &JMSSerializer:错误的 Json 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一个响应中,我得到一个字段作为对象,一个字段作为数组,我该如何纠正?

In the same response i get a field as object and a field as Array, how can i correct this ?

注意我不控制响应的构建,因为我使用的是 JMSSerializer

Note i don't controle the building of my response because i'm using JMSSerializer

代码(编辑 1)

\Entity\Profil.php

       class Profil
        {

        ...
        /**
             * Get actualites
             *
             * @return \Doctrine\Common\Collections\Collection 
             */
            public function getActualites()
            {
                return $this->actualites;
            }
...
/**
     * Add actualites
     *
     * @param \Genius\ProfileBundle\Entity\Actualite $actualites
     * @return Profil
     */
    public function addActualite(\Genius\ProfileBundle\Entity\Actualite $actualites)
    {
        $this->actualites[] = $actualites;

        return $this;
    }


    ...
    public function __construct()
        {
...         
         $this->actualites = new \Doctrine\Common\Collections\ArrayCollection();
...
        }

推荐答案

JsonResponse 使用 json_encode 对值进行编码.默认情况下 json_encode 会将 php 关联数组转换为 json 对象,而数字索引数组将保留为 json 数组.请参阅 PHP 文档,了解有关 json_encode 如何工作的更多信息.

The JsonResponse uses json_encode to encode the values. json_encode by default will convert a php associative array to a json object, and a numerically indexed array will stay as a json array. See the PHP docs for more info on how json_encode works.

我的建议是按照您想要的 json 的方式格式化您的 php.如果您只需要 json 中的数组,那么您应该仅使用数字索引数组来格式化您的 php 数据.如果您只想在 json 中使用对象,则将 php 数据格式化为仅使用关联数组.

My suggestion is to format your php in the way that you want your json. if you only want arrays in your json, then you should format your php data using only numerically indexed arrays. if you only want to use objects in your json, then format your php data to only use associative arrays.

示例:

$a = array(1,2,3);
echo json_encode($a); // [1,2,3] array output in json

$b = array('a'=>1, 'b'=>2, 'c'=>3);
echo json_encode($b); // {a:1,b:2,c:3} object output in json

看起来您正在使用 ArrayCollection,它在技术上是一个对象.如果要将 ArrayCollection 转换为简单数组,Doctrine 文档 向您展示了您可以调用 toArray() 方法.所以你会做这样的事情:

It looks like you are using an ArrayCollection, which is technically an object. If you want to convert an ArrayCollection to a simple array, the Doctrine docs show you that you can call a toArray() method. So you would do something like this:

$arrayValue = $object->getActualites()->toArray();

您还可以尝试其他一些方法,但它们可能会产生不必要的副作用.

There are a couple other things that you could try, but they might have unwanted side effects.

解决方案 1:您可以在您的实体中尝试此操作:

solution 1: you could try this in your entity:

public function getActualites()
{
    return $this->actualites->toArray();
}

这样,无论何时调用此方法,它都会返回一个数组而不是数组集合.这个解决方案会比下一个更好.

That way, whenever this method is called, it would return an array instead of array collection. This solution would be better than the next.

解决方案 2:另一种选择是使该变量成为数组本身.所以在构造函数中,你会有这个

Solution 2: The other option is to make that variable an array itself. So in the constructor, you would have this

public function __construct()
{       
     $this->actualites = array();
}

我还没有测试过这个解决方案,我不能 100% 确定 Doctrine 需要 Array Collection 与否,但你可以尝试作为最后的手段.

I haven't tested this solution, I'm not 100% sure if Doctrine needs the Array Collection or not, but you could try as a last resort.

这篇关于FOSRestBundle &JMSSerializer:错误的 Json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆