<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency> import org.json.JSONArray; import org.json.JSONObject; String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; JSONObject jsonObject = new JSONObject(jsonString); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.0</version> </dependency> 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); import org.json.JSONObject; JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "John"); jsonObject.put("age", 30); jsonObject.put("city", "New York"); String jsonString = jsonObject.toString(); System.out.println(jsonString); import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; ObjectMapper objectMapper = new ObjectMapper(); ObjectNode objectNode = objectMapper.createObjectNode(); objectNode.put("name", "John"); objectNode.put("age", 30); objectNode.put("city", "New York"); String jsonString = objectMapper.writeValueAsString(objectNode); System.out.println(jsonString);


上一篇:
下一篇:
切换中文