Java 中 JsonPatch 接口的重要性?

javajsonobject oriented programmingprogramming

JsonPatch  接口是一种用于存储可应用于目标 JSON 结构的操作序列的格式。添加、删除、替换、复制、移动测试等一些操作可以存储在JsonPath 中并在 JSON 结构上进行操作。JsonPatchBuilder  接口可用于使用Json.createPatchBuilder()构建 JSON 补丁。

JSON 文件

示例

import java.io.*;
import javax.json.Json;
import javax.json.JsonPatch;
import javax.json.JsonPatchBuilder;
import javax.json.JsonReader;
import javax.json.JsonStructure;
public class JsonPatchTest {
   public static void main(String[] args) throws Exception {
      JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();
      JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build();
      JsonReader reader = Json.createReader(new FileReader("simple.json"));
      JsonStructure jsonStructure1 = reader.read();
      JsonStructure jsonStructure2 = jsonPatch.apply(jsonStructure1);
      System.out.println(jsonStructure2);
      reader.close();
   }
}

输出

{"firstName":"Raja","lastName":"Ramesh","streetAddress":"Madhapur","city":"Hyderabad","state":"Telangana","phoneNumbers":[{"Mobile":"9959984000"},{"Home":"7702144400"}],"postalCode":"500072"}

相关文章