JSON Operation Techniques in Java in Java

JSON operating skills common in java Java is a widely used programming language, which is very suitable for processing various data formats, including JSON (JavaScript Object Notation).JSON is a lightweight data exchange format that is widely used in data transmission between web applications.In Java, there are many common JSON operating skills that can simplify the process of processing JSON data.This article will introduce some common JSON operation skills and provide corresponding Java code examples. 1. Analyze json data Analysis of JSON data is the process of converting it from the string format to the Java object.Java provides many libraries to analyze JSON data, the most commonly used are Jackson and GSON.The following is an example of using Jackson library to analyze JSON data: import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonString); String name = jsonNode.get("name").asText(); int age = jsonNode.get("age").asInt(); String city = jsonNode.get("city").asText(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); 2. Generate JSON data The process of generating JSON data is the process of converting Java to the JSON string.Similarly, Jackson and GSON libraries also provide the function of converting Java objects into json string.The following is an example of generating JSON data using GSON library: import com.google.gson.Gson; class Person { private String name; private int age; private String city; // Construct function, Getter, and Setter method omitted @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", city='" + city + '\'' + '}'; } } Person person = new Person(); person.setName("John"); person.setAge(30); person.setCity("New York"); Gson gson = new Gson(); String jsonString = gson.toJson(person); System.out.println(jsonString); 3. Modify json data A common way to modify JSON data in Java is to resolve the JSON string into a editing Java object, modify it, and then convert it back to the JSON strings.The following is an example of using the Jackson library to modify JSON data: import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonString); ((ObjectNode) jsonNode).put("age", 31); String modifiedJsonString = objectMapper.writeValueAsString(jsonNode); System.out.println(modifiedJsonString); In the above example, we modify the age to 31 and convert the modified JSON data into string. Summary: This article introduces JSON operating skills commonly in Java, including analyzing JSON data, generating JSON data, and modifying JSON data.Jackson and GSON are two commonly used libraries, which provide rich functions to process JSON data.By mastering these techniques, you can easily process and operate JSON data.