![](/img/trans.png)
[英]How to ignore pojo annotations while using Jackson ObjectMapper?
[英]JACKSON: How to ignore the POJO name while converting a POJO to JSON using Jackson?
I am using Jackson 2.10.1 library to convert my Java POJOs to JSON and I am getting the below output, I require the output without the POJO name(MyTestPojo here), I have tried various jackson annotations like @JsonIgnoreProperties but those are mostly for POJO 中存在的成员而不是 POJO class 名称。
{
"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 表示带注释的“getter”方法的结果(这意味着签名必须是 getter 的签名;非 void 返回类型,无参数)将用作实例序列化的单个值。 通常值是简单的标量类型(字符串或数字),但它可以是任何可序列化的类型(集合、Map 或 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();
}
并将其注册到您的 object 映射器并从主 Class 中删除@JsonValue
。
objectMapper.addMixIn(GetResponseVO.class, JsonMixin.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.