<dependency> <groupId>com.streametry</groupId> <artifactId>streametry-json</artifactId> <version>1.0.0</version> </dependency> import com.streametry.json.Json; import com.streametry.json.JsonObject; import com.streametry.json.JsonException; public class JsonParser { public static void main(String[] args) { String jsonStr = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; try { JsonObject jsonObject = Json.parse(jsonStr).asObject(); 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); } catch (JsonException e) { e.printStackTrace(); } } } import com.streametry.json.Json; import com.streametry.json.JsonObject; public class JsonCreator { public static void main(String[] args) { JsonObject jsonObject = new JsonObject(); jsonObject.add("name", "John"); jsonObject.add("age", 30); jsonObject.add("city", "New York"); String jsonStr = jsonObject.toString(); System.out.println(jsonStr); } } public class Person { @JsonProperty("name") private String name; @JsonProperty("age") private int age; @JsonProperty("city") private String city; // getters and setters } public class CustomSerializer implements JsonSerializer<CustomObject>, JsonDeserializer<CustomObject> { public JsonElement serialize(CustomObject customObject, Type type, JsonSerializationContext context) { } public CustomObject deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) { } } JsonParser jsonParser = new JsonParser(); jsonParser.setIgnoreUnknownProperties(true); jsonParser.setIgnoreNullValues(true);


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