Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json JMS将ArrayCollection序列化为对象_Json_Symfony_Serialization_Jmsserializerbundle - Fatal编程技术网

Json JMS将ArrayCollection序列化为对象

Json JMS将ArrayCollection序列化为对象,json,symfony,serialization,jmsserializerbundle,Json,Symfony,Serialization,Jmsserializerbundle,我正在使用JMS序列化程序。JsonSerializer在处理Doctrine ArrayCollection类型时给了我一个不正确的数组格式。spected结果应该遵循[{},{}]的格式,但是它给了我{1:{},2:{} 有关此场景的其他信息。仅当我尝试序列化包含ArrayCollection的对象且ArrayCollection包含第一级对象的对象时,才会发生这种情况。例如: { "description":"Text provided", "date":"14341459

我正在使用JMS序列化程序。JsonSerializer在处理Doctrine ArrayCollection类型时给了我一个不正确的数组格式。spected结果应该遵循
[{},{}]
的格式,但是它给了我
{1:{},2:{}

有关此场景的其他信息。仅当我尝试序列化包含ArrayCollection的对象且ArrayCollection包含第一级对象的对象时,才会发生这种情况。例如:

{  
   "description":"Text provided",
   "date":"1434145921000",
   "oid":1,
   "userCreator":{  
      "username":"name123",
      "password":"psw",
      "oid":2,
      "name":"the-name",
      "lastname":"the-lasname",
      "announcements":{  
         "1":{  
            "description":"Clases de inglés",
            "date":"1434745921000",
            "oid":3
         },
         "2":{  
            "description":"Reparar ordenador",
            "date":"1434145921000",
            "oid":5
         }
      }
   }
}
但是,如果我直接序列化用户实体,则不会发生这种情况:

{  
   "username":"user1",
   "password":"123",
   "oid":2,
   "name":"Rafael",
   "lastname":"Jimenez"
   "announcements":[  
      {  
         "description":"Cargar cajas a la guardilla",
         "date":"1434145921000",
         "oid":1
      },
      {  
         "description":"Contar césped y quitar malas hierbas",
         "date":"1434745921000",
         "oid":3
      },
      {  
         "description":"Reparar ordenador",
         "date":"1434145921000",
         "oid":5
      }
   ]
}

有什么线索吗?

您需要重置ArrayCollection数组中的键:

$associative = new ArrayCollection([0 => 1, 2 => 1]);
$list = new ArrayCollection($associative->getValues());
作为github上的@stof sad:

如果您的数组未使用从0到count($array)的序列编制索引- 1,获取JS对象而不是数组是预期的结果 行为,因为这是关联数组需要转换的方式 到JSON。如果键不是这样的序列,那么数组就是一个映射,而不是一个数组 名单

请看以下示例:

php > echo json_encode([0 => 1, 1 => 1]);
[1,1]
php > echo json_encode([0 => 1, 2 => 1]);
{"0":1,"2":1}

我认为序列化程序将检查数组是否为数字数组,并决定将其序列化为JS
array
Object

如果数组中有一个单元格为null,它将首先跳过该单元格,跳过该单元格会使索引中断,序列化程序将其识别为关联数组

array:28 [
  0 => "High Fashion"
  1 => "Contemporary"
  2 => "Streetwear"
  3 => null
  4 => "Grooming"
]
由于
$arr[3]
为空,因此跳过。一旦跳过,索引将停止。=>序列化器把它看作是一个关联数组。< /P>
array:28 [
  0 => "High Fashion"
  1 => "Contemporary"
  2 => "Streetwear"
  3 => null
  4 => "Grooming"
]
此情况的解决方案:

首先删除空单元格,然后获取数组值并序列化
array\u值(array\u filter($resources))

实体使用Doctrine ArrayCollection关联。所以序列化这种对象是非常常见的(在我看来)。此外,ArrayCollection听起来更像“数组”,而不是“关联数组”。你可以考虑其他方法来进行序列化。谢谢!知道了。您的示例位于一个简单的场景中。想象一下,当一个数组集合递归地包含在多个数组集合中时会发生什么。我可以为此编写一个模块,但是库应该处理这个问题,以支持基本类型作为数组集合。你同意吗?这取决于你如何得到关联数组。在您的示例中,“公告”缺少零索引。它被过滤了还是被删除了?如果是这样,您可以在筛选数组后立即应用密钥重置。如果有递归过滤,则需要根据过滤算法递归应用重置键。另一方面,您可以尝试递归地遍历生成的javascript对象并将其强制转换为列表。这取决于你到底想做什么。