用不同的键征求对象的符号化
从API中,我获得了这样的输出:
{
"type": "string",
"code": "string",
"addInfo2": "",
"addInfo3": "23536723462",
"addInfo4": null,
"addInfo5": null,
"arrow": "none",
"IdList": [
"2357789234"
],
"templateName": null,
"rotationDegrees": "0"
}
现在我想通过调用以下方式将此js缩合到一个对象中:
$ this-> serializer-> deserialize($ jsonlabelmappings,labelmappings :: class,'json');
但是我希望该对象具有其他键/属性。我的对象应该看起来像:
{
"type": "string",
"code": "string",
"originCountry": "", /* this is the addInfo2 */
"gtin": "23536723462", /* this is the ddInfo3 */
"wildfang": null, /* this is the addInfo4 */
"arrow": "none",
"ids": [ /* this is the articleIdList */
"2357789234"
],
"templateName": null,
"rotationDegrees": "0"
}
是否有@Serializer \ deserializename之类的阳性?还是如何告诉我的代码,JSON的Keyname是其他的?
From an API I get a output like that:
{
"type": "string",
"code": "string",
"addInfo2": "",
"addInfo3": "23536723462",
"addInfo4": null,
"addInfo5": null,
"arrow": "none",
"IdList": [
"2357789234"
],
"templateName": null,
"rotationDegrees": "0"
}
Now I want to deserialize this jsonstring into an Object by calling:
$this->serializer->deserialize($jsonLabelMappings, LabelMappings::class, 'json');
But I want that the Object has other keys/attributenames. My object should look like that:
{
"type": "string",
"code": "string",
"originCountry": "", /* this is the addInfo2 */
"gtin": "23536723462", /* this is the ddInfo3 */
"wildfang": null, /* this is the addInfo4 */
"arrow": "none",
"ids": [ /* this is the articleIdList */
"2357789234"
],
"templateName": null,
"rotationDegrees": "0"
}
Is there any anotation like @Serializer\DeserializeName or something? Or how can I tell my code that the keyName from the json is something other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确切的用例有序列化名称属性(或注释)。
< noreferrer“> https://symfony.com/doc/current/current/components/serializer.html#configure-name-conversion-conversion-using-metadata
另外,您可以使用SerializedPath属性
https://symfony.com/doc/current/serializer.html#using-嵌套属性
There is SerializedName attribute (or annotation) for the exact use case.
https://symfony.com/doc/current/components/serializer.html#configure-name-conversion-using-metadata
Alternatively you can use SerializedPath attribute
https://symfony.com/doc/current/serializer.html#using-nested-attributes
因为我没有得到任何答案,这些答案无法解决我的注释或“简短”的问题。我现在使用@fly_moe答案做到了:
通过将JSON变成数组应该很容易做到。循环通过数组,然后用新的键替换键。那就是我要做的
Because I didn't get any answer which woul solve my problem with annotations or something "short". I now did it with @Fly_Moe answer:
Should be easy to do by turning the json into an array. Loop through the array and replace the key with the new one. That's what I would do