使用 Jackson 将 Json 解析为 POJO
Parse Json into POJO using Jackson
我有以下json
{
"root": {
"status": "UP",
"connection1": {
"status": "UP"
},
"connection2": {
"status": "UP"
}
}
}
我还有以下 POJO classes 我想将 JSON 转换成
@JsonIgnoreProperties(ignoreUnknown = true)
public class POJO {
@JsonProperty("root")
@JsonDeserialize(using = RootDeserializer.class)
private Root root;
//getters + setters
}
public class Root {
private boolean isAlive;
private List<Connection> connections;
public Root(boolean isAlive, List<Connection> connections) {
this.isAlive = isAlive;
this.connections = connections;
}
//getters + setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Connection {
private String status;
//getters + setters
}
最后我有了这个反序列化器来将 json 转换为 Root 实例
public class RootDeserializer extends JsonDeserializer<Root> {
private static final String CONNECTION_PREFIX = "connection";
private static final String UP_STATUS = "UP";
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public Root deserialize(JsonParser parser, DeserializationContext context) throws IOException {
Map<String, Map<String, Object>> rootJsonMap = parser.readValueAs(Map.class);
boolean isAlive = StringUtils.equals(UP_STATUS, String.valueOf(rootJsonMap.get("status")));
List<Connection> connections = rootJsonMap.entrySet()
.stream()
.filter(entry -> StringUtils.startsWithIgnoreCase(entry.getKey(), CONNECTION_PREFIX))
.map(this::mapToConnection)
.collect(Collectors.toList());
return new Root(isAlive, connections);
}
private PosServerConnection mapToConnection(Map.Entry<String, Map<String, Object>> entry) {
Map<String, Object> connectionJsonMap = entry.getValue();
return objectMapper.convertValue(connectionJsonMap, Connection.class);
}
}
这样我就可以将我所有的连接分组到根 class 中的一个列表中。
我的问题是还有其他方法可以做到这一点吗??
我想在没有这么大的反序列化器的情况下只使用我的 Pojo 上的 Jackson 注释来做到这一点 classes
您可以简单地通过使用 @JsonAnySetter
注释为 List<Connection>
自定义 Setter
来实现这一点,如下所示。您也可以参考 Jackson Annotation Examples 来了解它是如何工作的。
POJO
public class Pojo {
private Root root;
//general getters, setters and toString
}
public class Root {
private String status;
private List<Connection> connections = new ArrayList<>();
public List<Connection> getConnections() {
return connections;
}
@JsonAnySetter
public void setConnections(String name, Connection connection) {
connection.setName(name);
this.connections.add(connection);
}
//other getters, setters and toString
}
public class Connection {
private String name;
private String status;
//general getters, setters and toString
}
然后你可以将给定的 JSON 字符串序列化为 Pojo
用普通的方式 Jackson
:
代码段
ObjectMapper mapper = new ObjectMapper();
Pojo pojo = mapper.readValue(jsonStr, Pojo.class);
System.out.println(pojo.getRoot().getConnections().toString());
控制台输出
[Connection [name=connection1, status=UP], Connection [name=connection2, status=UP]]
我有以下json
{
"root": {
"status": "UP",
"connection1": {
"status": "UP"
},
"connection2": {
"status": "UP"
}
}
}
我还有以下 POJO classes 我想将 JSON 转换成
@JsonIgnoreProperties(ignoreUnknown = true)
public class POJO {
@JsonProperty("root")
@JsonDeserialize(using = RootDeserializer.class)
private Root root;
//getters + setters
}
public class Root {
private boolean isAlive;
private List<Connection> connections;
public Root(boolean isAlive, List<Connection> connections) {
this.isAlive = isAlive;
this.connections = connections;
}
//getters + setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Connection {
private String status;
//getters + setters
}
最后我有了这个反序列化器来将 json 转换为 Root 实例
public class RootDeserializer extends JsonDeserializer<Root> {
private static final String CONNECTION_PREFIX = "connection";
private static final String UP_STATUS = "UP";
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public Root deserialize(JsonParser parser, DeserializationContext context) throws IOException {
Map<String, Map<String, Object>> rootJsonMap = parser.readValueAs(Map.class);
boolean isAlive = StringUtils.equals(UP_STATUS, String.valueOf(rootJsonMap.get("status")));
List<Connection> connections = rootJsonMap.entrySet()
.stream()
.filter(entry -> StringUtils.startsWithIgnoreCase(entry.getKey(), CONNECTION_PREFIX))
.map(this::mapToConnection)
.collect(Collectors.toList());
return new Root(isAlive, connections);
}
private PosServerConnection mapToConnection(Map.Entry<String, Map<String, Object>> entry) {
Map<String, Object> connectionJsonMap = entry.getValue();
return objectMapper.convertValue(connectionJsonMap, Connection.class);
}
}
这样我就可以将我所有的连接分组到根 class 中的一个列表中。 我的问题是还有其他方法可以做到这一点吗??
我想在没有这么大的反序列化器的情况下只使用我的 Pojo 上的 Jackson 注释来做到这一点 classes
您可以简单地通过使用 @JsonAnySetter
注释为 List<Connection>
自定义 Setter
来实现这一点,如下所示。您也可以参考 Jackson Annotation Examples 来了解它是如何工作的。
POJO
public class Pojo {
private Root root;
//general getters, setters and toString
}
public class Root {
private String status;
private List<Connection> connections = new ArrayList<>();
public List<Connection> getConnections() {
return connections;
}
@JsonAnySetter
public void setConnections(String name, Connection connection) {
connection.setName(name);
this.connections.add(connection);
}
//other getters, setters and toString
}
public class Connection {
private String name;
private String status;
//general getters, setters and toString
}
然后你可以将给定的 JSON 字符串序列化为 Pojo
用普通的方式 Jackson
:
代码段
ObjectMapper mapper = new ObjectMapper();
Pojo pojo = mapper.readValue(jsonStr, Pojo.class);
System.out.println(pojo.getRoot().getConnections().toString());
控制台输出
[Connection [name=connection1, status=UP], Connection [name=connection2, status=UP]]