如何在批量上更改JSON字段名称

发布于 2025-01-26 21:58:05 字数 300 浏览 1 评论 0 原文

我在hashmap< string,object>中有一些json。而且我正在使用对象拍摄器将其映射为JSON。我需要更改几乎所有值的字段名称。 前任: 将其更改

{ “field1”:”abc”, “field2”: “xyz”}

{“f1”:”abc”, “f2”:”xyz”}

我阅读的有关使用Jackson并使用注释@jsonproperty的信息,但是就我而言,这是不可行的,因为我需要更改至少20多个字段的名称。最好的方法是什么?

I have some json in a hashmap<string, object> and i’m using ObjectMapper to map it json. I need to change the field names of pretty much all of the values.
ex:
change this

{ “field1”:”abc”, “field2”: “xyz”}

to this

{“f1”:”abc”, “f2”:”xyz”}

I’ve read about using Jackson and using annotation @JsonProperty, but in my case it’s just not feasible because i need to change the names of atleast 20+ fields. What’s the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

哑剧 2025-02-02 21:58:05

您为什么不能更改您的inot源?如此巨大的变化是不必要的,征税

Why can’t you change ur inout source? Such large change is unnecessary and taxing

鸠书 2025-02-02 21:58:05

如果您使用objectMapper解析此JSON,则获得中间DTO后,可以将Mixin并导出到字符串中,然后转换为必需的DTO。

  • 使用 field1 将字符串转换为
  • 将jackson mixin设置为对象mapper
ObjectMapper mapper = new ObjectMapper();

@Getter
@Setter
public abstract class FinalMixin {
  
  @JsonProperty("f1")
  private String field1;
}

mapper.addMixInAnnotations(OriginalDTO.class, FinalMixin.class);
  • 转换dto,您将进入步骤1进入字符串
  • 转换结果字符串到 finaldto

If you are parsing this Json with ObjectMapper, after getting intermediate DTO you can apply Mixin and export into String and then convert to required DTO.

  • Convert String to OriginalDTO with field1
  • Set Jackson mixin to object mapper
ObjectMapper mapper = new ObjectMapper();

@Getter
@Setter
public abstract class FinalMixin {
  
  @JsonProperty("f1")
  private String field1;
}

mapper.addMixInAnnotations(OriginalDTO.class, FinalMixin.class);
  • Convert DTO that you get in step 1 into String
  • Convert result String into FinalDTO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文