我正在尝试将一个json字符串反序列化为Java对象。下面是我的json字符串。
{
"header": {
"transactionId": "12345",
"application": "testApp"
},
"items": {
"item": [
{
"attributes": {
"attribute": [
{
"key": "accountType",
"value": "TYPE1"
},
{
"key": "accountId",
"value": "123"
}
]
}
},
{
"attributes": {
"attribute": [
{
"key": "userType",
"value": "TYPE2"
},
{
"key": "userId",
"value": "321"
}
]
}
}
]
}
}
我想将这个json反序列化为Java类,如下所示。
public class Response {
private Header header;
private List<Object> items;
//getters and setters
}
public class Header {
private String transactionId;
private String application;
//getters and setters
}
public class Account {
private String accountType;
private String accountId;
//getters and setters
}
public class User {
private String userType;
private String userId;
//getters and setters
}
如何使用Jackson或objectmapper等进行反序列化?主要问题是字段名在属性对象和“key”字段的值中。是否可以使用“key”字段的值在Java对象上找到正确的字段,并使用“value”字段的值设置正确的值?
1条答案
按热度按时间lymnna711#
一种可能的解决方案是在转换为POJO之前转换JSON结构。
https://github.com/octomix/josson
产出