import com.streametry.gson.JsonArray; import com.streametry.gson.JsonElement; import com.streametry.gson.JsonObject; import com.streametry.gson.JsonParser; String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; JsonElement element = JsonParser.parseString(json); JsonObject object = element.getAsJsonObject(); String name = object.get("name").getAsString(); int age = object.get("age").getAsInt(); String city = object.get("city").getAsString(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); import com.streametry.gson.JsonObject; import com.streametry.gson.JsonPrimitive; import com.streametry.gson.JsonStream; public class Person { private String name; private int age; private String city; public JsonObject toJson() { JsonObject json = new JsonObject(); json.addProperty("name", name); json.addProperty("age", age); json.addProperty("city", city); return json; } } Person person = new Person("John", 30, "New York"); JsonObject json = person.toJson(); JsonStream stream = new JsonStream(); stream.write(json); String jsonString = stream.toString(); System.out.println(jsonString);


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