{"before":null,"after":{"ID":"Bw==","NAME":"Product 3","DESCRIPTION":"Description 3","WEIGHT":"FkQ="},"source":{"version":"1.6.4.Final","connector":"oracle","name":"oracle_logminer","ts_ms":1686274678794,"snapshot":"true","db":"ORA11G","sequence":null,"schema":"C##TEST123","table":"PRODUCTS","txId":null,"scn":"2202609","commit_scn":null,"lcr_position":null},"op":"r","ts_ms":1686274678794,"transaction":null}将上面json中间 after 对应的json使用java代码提取出来
时间: 2023-07-08 20:45:56 浏览: 85
可以使用 Java 中的 JSON 解析库(如 Jackson、Gson 等)来解析这个 JSON 字符串,然后提取其中的`after`字段对应的 JSON 对象。以下是使用 Jackson 库实现的示例代码:
```java
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
// 定义 JSON 字符串
String jsonString = "{\"before\":null,\"after\":{\"ID\":\"Bw==\",\"NAME\":\"Product 3\",\"DESCRIPTION\":\"Description 3\",\"WEIGHT\":\"FkQ=\"},\"source\":{\"version\":\"1.6.4.Final\",\"connector\":\"oracle\",\"name\":\"oracle_logminer\",\"ts_ms\":1686274678794,\"snapshot\":\"true\",\"db\":\"ORA11G\",\"sequence\":null,\"schema\":\"C##TEST123\",\"table\":\"PRODUCTS\",\"txId\":null,\"scn\":\"2202609\",\"commit_scn\":null,\"lcr_position\":null},\"op\":\"r\",\"ts_ms\":1686274678794,\"transaction\":null}";
// 创建 ObjectMapper 对象
ObjectMapper mapper = new ObjectMapper();
// 解析 JSON 字符串
JsonNode rootNode = mapper.readTree(jsonString);
// 提取 after 对应的 JSON 对象
JsonNode afterNode = rootNode.get("after");
// 输出提取结果
System.out.println(afterNode.toString());
```
最终输出结果为:
```
{"ID":"Bw==","NAME":"Product 3","DESCRIPTION":"Description 3","WEIGHT":"FkQ="}
```
阅读全文