<dependency> <groupId>org.apache.johnzon</groupId> <artifactId>johnzon-core</artifactId> <version>1.2.2</version> </dependency> implementation 'org.apache.johnzon:johnzon-core:1.2.2' JsonBuilderFactory factory = Json.createBuilderFactory(Collections.emptyMap()); JsonObject jsonObject = factory.createObjectBuilder() .add("name", "John") .add("age", 25) .add("isStudent", true) .build(); JsonWriterFactory writerFactory = Json.createWriterFactory(Collections.emptyMap()); try (JsonWriter writer = writerFactory.createWriter(System.out)) { writer.writeObject(jsonObject); } String json = "{\"name\":\"John\", \"age\":25, \"isStudent\":true}"; JsonReaderFactory readerFactory = Json.createReaderFactory(Collections.emptyMap()); try (JsonReader reader = readerFactory.createReader(new StringReader(json))) { JsonObject jsonObject = reader.readObject(); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); boolean isStudent = jsonObject.getBoolean("isStudent"); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Is Student: " + isStudent); }


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