共計(jì) 553 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Java 中使用 json-path 庫進(jìn)行刪除指定路徑數(shù)據(jù),可以通過以下步驟實(shí)現(xiàn):
- 使用 json-path 庫解析 JSON 數(shù)據(jù),例如使用 com.jayway.jsonpath.JsonPath 類的靜態(tài)方法 parse() 方法解析 JSON 字符串:
String jsonStr = "{\"name\": \"John\", \"age\": 30, \"address\": {\"city\": \"New York\", \"zip\": \"10001\"}}";
DocumentContext jsonContext = JsonPath.parse(jsonStr);
- 使用 json-path 的 delete() 方法刪除指定路徑數(shù)據(jù),例如使用 delete() 方法刪除指定路徑的數(shù)據(jù):
jsonContext.delete("$.address.city");
- 獲取更新后的 JSON 數(shù)據(jù),例如使用 jsonContext.jsonString() 方法獲取更新后的 JSON 字符串:
String updatedJsonStr = jsonContext.jsonString();
System.out.println(updatedJsonStr);
通過以上步驟,可以使用 json-path 庫在 Java 中刪除指定路徑的數(shù)據(jù)。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完