1 . 比原生的json解析生成数据简单
package com.qf.demo4;
import com.google.gson.Gson;
import com.qf.demo.Person;
public class Test {
public static void main(String[] args) {
Gson gson = new Gson();
Person person = new Person("zhangsan", 20);
String string = gson.toJson(person);
System.out.println(string);
}
}
2 .
package com.qf.demo4;
import com.google.gson.Gson;
import com.qf.demo3.Dept;
import com.qf.demo3.Person;
public class Test2 {
public static void main(String[] args) {
Person person = new Person("zhangsan", 10, new Dept(2, "hehe"));
Gson gson = new Gson();
String json = gson.toJson(person);
System.out.println(json);
}
}