I want to deserialize something like this:
[
{ "id": 42 },
{ "id": 43 }
]
Any idea how to do this?
I want to deserialize something like this:
[
{ "id": 42 },
{ "id": 43 }
]
Any idea how to do this?
It would be
$serializer->deserialize($json, 'array<T>', 'json')
where T
is the name of the class with the id
property.
[123=> { "id": 42 }, 234=>{ "id": 42 }]
) within the array? This worked for me but the keys are reset (starting from 0) –
Otto $this->serializer->deserialize($result, 'array<integer,MyModelDefinedInYML>>', 'json')
–
Otto Let's say you have a class foo, that has an attribute with an array of bar objects.
In your foo class use JMS\Serializer\Annotation\Type as Type;
and annotate the attribute like this:
use JMS\Serializer\Annotation\Type as Type;
class foo {
/**
*
* @Type("array<int,Namespace\To\Class\Bar>")
* private $bars = array();
*/
}
JMS Serializer will serialize/deserialize the contents of foo automagically. This use of @Type should be utilized for all attributes of any classes you will be serializing/deserializing.
This also works in situations where you are using string keys (ie. associative array structure). Just substitute "string" for int.
@Type("array<string,Namespace\To\Class\Bar>")
© 2022 - 2024 — McMap. All rights reserved.
@Type("array<Class>")
but it does not work since the array should have a key to work – Hydric$serializer->deserialize($json, 'array<T>', 'json');
? WhereT
is the type withid
property. – Passbookdeserialize($json, "array<Class>")
you can post the answer if you want. – Hydric