- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Jackson 2.10.1 库将我的 Java POJO 转换为 JSON,我得到以下输出,我需要没有 POJO 名称的输出(此处为 MyTestPojo),我尝试了各种 jackson 注释,如 @JsonIgnoreProperties,但那些主要用于 POJO 中存在的成员,而不是 POJO 类名。
{
"MyTestPojo": [
{
"CreatedBy": "user1",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": true,
"Value": "testABC1",
"CreatedDate": "2017-03-13 15:41:54.0",
"Description": "testABC"
},
{
"CreatedBy": "user2",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": false,
"Value": "testABC2",
"CreatedDate": "2017-03-13 15:41:54.0",
"Description": "testABC"
}
]
}
而我需要的是 -
[
{
"CreatedBy": "user1",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": true,
"Value": "testABC1",
"CreatedDate": "2019-03-13 15:41:54.0",
"Description": "testABC"
},
{
"CreatedBy": "user2",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": false,
"Value": "testABC2",
"CreatedDate": "2020-03-10 15:41:54.0",
"Description": "testABC"
}
]
}
有没有办法用 Jackson 注释来处理这个问题?
我用过的POJO是-
@JacksonXmlRootElement(localName = "ArrayOfTestPojos")
public class GetResponseVO {
@JsonProperty("MyTestPojo")
@JacksonXmlProperty(localName = "MyTestPojo")
@JacksonXmlElementWrapper(useWrapping = false)
private ArrayList<MyTestPojo> MyTestPojoList;
public ArrayList<MyTestPojo> getMyTestPojoList() {
return MyTestPojoList;
}
public void setMyTestPojoList(ArrayList<MyTestPojo> MyTestPojoList) {
this.MyTestPojoList = MyTestPojoList;
}
// standard getters and setters
}
和
@JacksonXmlRootElement(localName = "MyTestPojo")
public class MyTestPojo {
@JsonProperty("Name")
private String name;
@JsonProperty("Description")
private String description;
@JsonProperty("IsActive")
private int isActive;
@JsonProperty("Value")
private String value = null;
@JsonProperty("CreatedBy")
private String createdBy;
@JsonProperty("CreatedDate")
private String createdDate;
@JsonProperty("UpdatedBy")
private String updatedBy;
@JsonProperty("UpdatedDate")
private String updatedDate;
// standard getters and setters.
}
```````````
I am also generating the XML out of this so you can ignore the annotations relevant to XML.
最佳答案
您可以为此目的使用 JsonValue
注释,基本上是“使用此属性的值而不是序列化容器对象”。它也可以用在 getters
上
@JsonValue indicates that results of the annotated "getter" method (which means signature must be that of getters; non-void return type, no args) is to be used as the single value to serialize for the instance. Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean).
@JsonValue
@JacksonXmlProperty(localName = "MyTestPojo")
@JacksonXmlElementWrapper(useWrapping = false)
private ArrayList<MyTestPojo> MyTestPojoList;
但这是错误的做法,因为它会生成这样的 JSON,这不是合法的 JSON
。
{[{"x":"value"}, ...]}
如果你只想改变 JSON 结构(不影响 xml),你可以使用 MixIn
来达到这个目的。
public interface JsonMixin {
@JsonValue
List<MyTestPojo> getMyTestPojoList();
}
并将其注册到您的对象映射器并从主类中删除@JsonValue
。
objectMapper.addMixIn(GetResponseVO.class, JsonMixin.class);
关于java - jackson : How to ignore the POJO name while converting a POJO to JSON using Jackson?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61932106/
我是一名优秀的程序员,十分优秀!