gpt4 book ai didi

java - 如何将Java对象转换为Json格式化属性名

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:06 28 4
gpt4 key购买 nike

我目前正在将 Rest 服务从 RestExpress 迁移到 Jersey 框架,我必须在其中具有与 RestExpress 相同的输出。

public class AnnouncementDTO {

private String id;
private String title;
private String details;
private String postedBy;

private String permanent;
private String dismissible;

private String startDate;
private String endDate;

}

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(announcementDTO );

输出:

{
"id" : null,
"title" : "<font size=\"3\" color=\"red\">This is some text!</font>",
"details" : "<p>fhmdhd</p>",
"postedBy" : "Portal, Administrator",
"permanent" : null,
"dismissible" : null,
"startDate" : "Jul 19, 2014, 04:44 AM IST",
"endDate" : null,
"read" : null
}

我的要求是将属性名称格式化为 postedByposted_by。因此预期结果如下。

{
"title":"<font size=\"3\" color=\"red\">This is some text!</font>",
"details":"<p>fhmdhd</p>",
"posted_by":"Portal, Administrator",
"start_date":"Jul 19, 2014, 04:44 AM ET"
}

最佳答案

@JsonProperty("posted_by")
private String postedBy;

关于java - 如何将Java对象转换为Json格式化属性名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386342/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com