用不同的键征求对象的符号化

发布于 2025-02-12 15:03:30 字数 904 浏览 3 评论 0原文

从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2025-02-19 15:03:31

确切的用例有序列化名称属性(或注释)。

< 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

霞映澄塘 2025-02-19 15:03:30

因为我没有得到任何答案,这些答案无法解决我的注释或“简短”的问题。我现在使用@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文