<dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.8</version> </dependency> </dependencies> import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonArray; import com.google.gson.JsonElement; Gson gson = new Gson(); String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"; JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class); String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); String city = jsonObject.get("city").getAsString(); import java.io.FileReader; FileReader reader = new FileReader("data.json"); JsonObject jsonObject = gson.fromJson(reader, JsonObject.class); String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); String city = jsonObject.get("city").getAsString(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city);


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