symfony2:JMSSerializerBundle将属性名称从"className"更改为改为"class_name"; [英] symfony2: JMSSerializerBundle changes the attribute name from "className" to "class_name"

查看:95
本文介绍了symfony2:JMSSerializerBundle将属性名称从"className"更改为改为"class_name";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JMSSerializerBundle序列化我的实体. 但是我遇到以下问题:属性名称是"className",但是在我的Json对象中却得到了"class_name".

I'm using the JMSSerializerBundle to serialize my entity. but I have the following problem: the attribute name is "className" but in my Json object I get a "class_name".

这是我的实体

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

 ...

 /**
  * @var string
  *
  * @ORM\Column(name="className", type="string", length=255)
  */
 private $className;

 /**
  * Set className
  *
  * @param string $className
  * @return Events
  */
 public function setClassName($className)
 {
     $this->className = $className;

     return $this;
 }

 /**
  * Get className
  *
  * @return string 
  */
 public function getClassName()
 {
     return $this->className;
 }
 ...
}

这是我的控制器

  class myController extends Controller{

      public function loadAction($action){
        $request=$this->get('request');
        if($request->isXmlHttpRequest())
        {
         switch( $action ) {

            case 'load':
                 $resultat=$this->getDoctrine()->getManager()->getRepository('ECMUserBundle:Events')
                    ->findAll();
                $serializer = $this->get('jms_serializer');
                $resultat=$serializer->serialize($resultat, 'json');
                echo $resultat;
                exit();
                break;
            ...

这是我的杰森

 [{"id":90,"title":"holliday","start":"2014-03-25T01:00:00+0000","end":"2014-03-25T01:00:00+0000","class_name":"label-orange","allday":"true"}]

这是逻辑行为吗?

推荐答案

检查文档中的@SerializedName批注:

Check the documentation for the @SerializedName annotation:

http://jmsyst.com/libs/serializer/master/reference/annotations

@SerializedName:

@SerializedName:

可以在属性上定义此注释,以定义属性的序列化名称. 如果未定义此属性,则该属性将从驼峰大写转换为带小写的带下划线的名称,例如camelCase-> camel_case.

这篇关于symfony2:JMSSerializerBundle将属性名称从"className"更改为改为"class_name";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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