{"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 10:48:27 浏览: 91
您可以使用Java的org.json库来解析JSON字符串,然后使用getJSONObject方法获取指定的JSONObject对象。以下是一个例子:
```java
import org.json.*;
public class JSONParser {
public static void main(String[] args) {
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}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject afterObject = jsonObject.getJSONObject("after");
String id = afterObject.getString("ID");
String name = afterObject.getString("NAME");
String description = afterObject.getString("DESCRIPTION");
String weight = afterObject.getString("WEIGHT");
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Description: " + description);
System.out.println("Weight: " + weight);
}
}
```
运行该程序,输出如下:
```
ID: Bw==
Name: Product 3
Description: Description 3
Weight: FkQ=
```
以上代码中,首先需要导入org.json库。然后,将JSON字符串传给JSONObject构造函数,然后使用getJSONObject方法获取指定的JSONObject对象,再使用getString方法获取该对象中的值。
阅读全文