php Symfony serializer -将xml反序列化为对象数组
如何将带有属性的xml反序列化为对象数组?
$string = '<?xml version="1.0" encoding="UTF-8" ?>
<response>
<item flight="23"/>
<item flight="24"/>
</response>';
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
$serializer = new Serializer(
[new ArrayDenormalizer(), new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)],
[new XmlEncoder()]
);
$objects = $serializer->deserialize($string, 'App\Entities\Item[]', 'xml');
物品类别:
class Item
{
#[SerializedName('@flight')]
public string $flight;
}
现在结果:
array:1 [
"item" => App\Entities\Item
]
1条答案
按热度按时间wkftcu5l1#
必须输入两次方括号。
或者下面的。但基本上是一回事。