使用Gson进行数据转换

注意:是否实例化Gson对象都可以实现转换的操作

Map to String

Map<String, String> data = new HashMap<>();
data.put("name", "edmond");
data.put("age", "22");
data.put("email", "[email protected]");
System.out.println(data); // {name=edmond, age=22, [email protected]}

String jsonStr = new Gson().toJson(data);
System.out.println(jsonStr); // {"name":"edmond","age":"22","email":"[email protected]"}

String to JsonObject

String jsonStr = "{\"name\":\"edmond\",\"age\":\"22\",\"email\":\"[email protected]\"}";
JsonObject jsonObject = new Gson().fromJson(jsonStr, JsonObject.class);
System.out.println(jsonObject); // {"name":"edmond","age":"22","email":"[email protected]"}

String to Bean类

class User 

你可能感兴趣的:(JavaWeb学习,java,json)