这篇文章我是怀着无比激动的心情写得,为啥为啥?因为都要放假了!你们的小心心是不是已经躁动不安了!族长考虑到现在离高速免费通行还有几个小时,大家也闲来无事,在下就写点让小伙伴们打发打发时间!今天我们就看一点通过Gson-2.3.1.jar封装的工具类!希望有助于你(温馨提醒:别看的忘了游玩出行的时间哦~~)
工具类
package com.sunfreeter.util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
*
* @author freeter
* @desc Json封装工具类简洁版
*/
public class JsonUtil {
/**@author 订阅号:sunfreeter
* @desc 对象转Json串
* @param object
* @return
*/
public static String toString(Object object){
String jsonString = new Gson().toJson(object);
return jsonString;
}
/**@author 订阅号:sunfreeter
* @desc Json串转自定义对象
* @param jsonString
* @param object
* @return
*/
public static <T> T stringToObject(String jsonString,Class<T> object){
T toObject = new Gson().fromJson(jsonString,object);
return toObject;
}
public static JsonObject stringToJosnObject(String jsonString){
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(jsonString);
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject;
}
public static String jsonObjectToJsonString(JsonObject jsonObject){
return jsonObject.toString();
}
}
测试实体类-MyClass
package com.sunfreeter.model;
import java.util.List;
/**
*
* @author sunfreeter
* @desc 班级
*/
public class MyClass {
private String code;
private String name;
private List<Student> students;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
测试实体类-Student
package com.sunfreeter.model;
/**
*
* @author sunfreeter
* @desc 学生
*/
public class Student {
private String sno;
private String sname;
private int age;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试主入口
package com.sunfreeter.app;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.JsonObject;
import com.sunfreeter.model.MyClass;
import com.sunfreeter.model.Student;
import com.sunfreeter.util.JsonUtil;
/**
*
* @author 订阅号:sunfreeter
* @desc 测试主入口
*/
public class Test {
public static void main(String[] args) {
Student student=new Student();
student.setSno("s_001");
student.setSname("张三");
student.setAge(20);
Student student1=new Student();
student1.setSno("s_002");
student1.setSname("李四");
student1.setAge(30);
List<Student> list=new ArrayList<Student>();
list.add(student);
list.add(student1);
MyClass mc=new MyClass();
mc.setCode("c_0001");
mc.setName("一年级");
mc.setStudents(list);
String ss=JsonUtil.toString(mc);
System.out.println(ss);
MyClass myClass = JsonUtil.stringToObject(ss, MyClass.class);
System.out.println(myClass.getCode());
JsonObject jsonObject= JsonUtil.stringToJosnObject(ss);
System.out.println(jsonObject.get("code").getAsString());
System.out.println(jsonObject.get("students").getAsJsonArray());
String ddd = JsonUtil.jsonObjectToJsonString(jsonObject);
System.out.println(ddd);
}
}