树枝json_encode空json [英] twig json_encode empty json

查看:111
本文介绍了树枝json_encode空json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从API接收元素列表.所有元素的格式都正确.当我用细枝甩掉其中之一时,我得到以下信息:

I'm receiving a list of elements from an API. All the elements are well formatted. When I dump one of them using twig, I get the following :

Leg {#2695 ▼
-id: null
#reservation: null
-airportStart: "AIX LES MILLES"
-airplaneType: "Cessna Citation Mustang"
-airportEnd: "ROBINSON"
-startDate: "2015-09-10 20:00:00"
-startHour: "2015-09-10 20:00:00"
-endHour: "2015-09-10 21:00:21"
-durationLeg: "01:21"
#nbPax: "4"
-price: null
-updatedPrice: null
-discountOnLeg: null
-tva: null
-status: null
}

我的用户必须选择这些元素之一,所以我想做的是使用

My user must select one of these elements, So what I'm trying to do is to send the encoded json back to the controller, using

{{ element|json_encode }}

不幸的是,json为空.当我尝试使用

Unfortunately, the json is empty. When I try to dump the encoded json using

{{ dump(element|json_encode) }}

我得到的只是一个空数组{};

all I get is an empty array {};

任何想法为什么还有另一种方法将选定的元素数据发送到控制器功能? (这些元素不会保留,API上的每次调用都会返回数千个结果)

Any idea why Is there another way to send the selected element datas to a controller function? (These elements are not persisted, each call on the API returns thousands of results)

推荐答案

我参加聚会的时间不算太晚(迟到了2年),但是对于像我这样的人来说,我来自谷歌研究中心,我说:我也有同样的经历也是问题,并且在四处搜寻之后,我通过 Serializer组件.如何?让我告诉你!

I'm little late to the party (2 years of lateness), but for any one like me coming from a google research i say : i had the same problem too, and after googling around, i "solved" my problem with the Serializer Component. How? let me show you!

安装

php composer.phar require symfony/serializer

实体

<?php

namespace Your\Namespace\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

/**
 * Leg
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Your\Namespace\Entity\LegRepository")
 */
class Leg {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ...

    public function serializer()
    {
        $encoder    = new JsonEncoder();
        $normalizer = new ObjectNormalizer();

        $normalizer->setIgnoredAttributes(array(
            'whatever', 'attributes', 'you', 'want', 'to', 'ignore'
        ));

        // The setCircularReferenceLimit() method of this normalizer sets the number 
        // of times it will serialize the same object 
        // before considering it a circular reference. Its default value is 1.
        $normalizer->setCircularReferenceHandler(function ($object) {
            return $object->getName();
        });

        $serializer = new Serializer(array($normalizer), array($encoder));
        return $serializer->serialize($this, 'json');
    }
}

嫩枝

{{ Leg.serializer|raw }}

NB :已在symfony 2.6下进行了测试

N.B : this is tested under symfony 2.6

这篇关于树枝json_encode空json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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