JsonPatch 接口在 Java 中的重要性是什么?
JsonPatch 接口是用于存储一串操作的格式,可以应用到目标 JSON 结构中。有几个操作,如add、remove、replace、copy、move和test,可以存储在 JsonPatch 中并对 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"}
广告