gson对json与对象与list集合的转换

时间:2022-12-19 13:53:22
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;

public class TestJson {
public static void main(String[] args) {

// 某对象转换成json
// test();
// json 转换成对象
// test1();
// 将集合转换成json格式字符串
test2();
}

private static void test2() {
// 初始化数据对象
ArrayList<Person> arrayList = new ArrayList<Person>();
JsonArray jsonArray = new JsonArray();
Person person = null;

// 初始化arraylist容器 100个person对象
for (int i = 0; i < 100; i++) {
person = new Person();
person.setAge(i + "age");
person.setName(i + "name");
arrayList.add(person);
}

Gson gson = new Gson();
// 将list集合变成json格式
String str = gson.toJson(arrayList);
System.out.println(str);
// 将str json格式变成 list格式
List<Person> list = gson.fromJson(str, new TypeToken<List<Person>>() {
}.getType());
// 输出list集合
for (int i = 0; i < 10; i++) {
System.out.println(list.get(i).getName());
}
}

private static void test() {
// 封装 person对象
Person person = new Person();
person.setAge("age");
person.setName("name");
person.setSex("sex");
// 创建gson对象
Gson gson = new Gson();
// 使用gson tojson方法将 person对象转换成json
String str = gson.toJson(person);
// 输出转换后的格式
System.out.println(str);

}

private static void test1() {
// json格式数据
String str = "{\"name\":\"name\",\"sex\":\"sex\",\"age\":\"age\"}";
// 输出信息
System.out.println(str);
// 创建gson对象
Gson gson = new Gson();
// 使用gson对象 fromJson方法 将 json转换成person对象
Person person = gson.fromJson(str, Person.class);
// 输出对象的属性
System.out.println("person age: " + person.getAge());
}
}


输出结果

[{"name":"0name","age":"0age"},{"name":"1name","age":"1age"},{"name":"2name","age":"2age"},{"name":"3name","age":"3age"},{"name":"4name","age":"4age"},{"name":"5name","age":"5age"},{"name":"6name","age":"6age"},{"name":"7name","age":"7age"},{"name":"8name","age":"8age"},{"name":"9name","age":"9age"},{"name":"10name","age":"10age"},{"name":"11name","age":"11age"},{"name":"12name","age":"12age"},{"name":"13name","age":"13age"},{"name":"14name","age":"14age"},{"name":"15name","age":"15age"},{"name":"16name","age":"16age"},{"name":"17name","age":"17age"},{"name":"18name","age":"18age"},{"name":"19name","age":"19age"},{"name":"20name","age":"20age"},{"name":"21name","age":"21age"},{"name":"22name","age":"22age"},{"name":"23name","age":"23age"},{"name":"24name","age":"24age"},{"name":"25name","age":"25age"},{"name":"26name","age":"26age"},{"name":"27name","age":"27age"},{"name":"28name","age":"28age"},{"name":"29name","age":"29age"},{"name":"30name","age":"30age"},{"name":"31name","age":"31age"},{"name":"32name","age":"32age"},{"name":"33name","age":"33age"},{"name":"34name","age":"34age"},{"name":"35name","age":"35age"},{"name":"36name","age":"36age"},{"name":"37name","age":"37age"},{"name":"38name","age":"38age"},{"name":"39name","age":"39age"},{"name":"40name","age":"40age"},{"name":"41name","age":"41age"},{"name":"42name","age":"42age"},{"name":"43name","age":"43age"},{"name":"44name","age":"44age"},{"name":"45name","age":"45age"},{"name":"46name","age":"46age"},{"name":"47name","age":"47age"},{"name":"48name","age":"48age"},{"name":"49name","age":"49age"},{"name":"50name","age":"50age"},{"name":"51name","age":"51age"},{"name":"52name","age":"52age"},{"name":"53name","age":"53age"},{"name":"54name","age":"54age"},{"name":"55name","age":"55age"},{"name":"56name","age":"56age"},{"name":"57name","age":"57age"},{"name":"58name","age":"58age"},{"name":"59name","age":"59age"},{"name":"60name","age":"60age"},{"name":"61name","age":"61age"},{"name":"62name","age":"62age"},{"name":"63name","age":"63age"},{"name":"64name","age":"64age"},{"name":"65name","age":"65age"},{"name":"66name","age":"66age"},{"name":"67name","age":"67age"},{"name":"68name","age":"68age"},{"name":"69name","age":"69age"},{"name":"70name","age":"70age"},{"name":"71name","age":"71age"},{"name":"72name","age":"72age"},{"name":"73name","age":"73age"},{"name":"74name","age":"74age"},{"name":"75name","age":"75age"},{"name":"76name","age":"76age"},{"name":"77name","age":"77age"},{"name":"78name","age":"78age"},{"name":"79name","age":"79age"},{"name":"80name","age":"80age"},{"name":"81name","age":"81age"},{"name":"82name","age":"82age"},{"name":"83name","age":"83age"},{"name":"84name","age":"84age"},{"name":"85name","age":"85age"},{"name":"86name","age":"86age"},{"name":"87name","age":"87age"},{"name":"88name","age":"88age"},{"name":"89name","age":"89age"},{"name":"90name","age":"90age"},{"name":"91name","age":"91age"},{"name":"92name","age":"92age"},{"name":"93name","age":"93age"},{"name":"94name","age":"94age"},{"name":"95name","age":"95age"},{"name":"96name","age":"96age"},{"name":"97name","age":"97age"},{"name":"98name","age":"98age"},{"name":"99name","age":"99age"}]

0name

1name

2name

3name

4name

5name

6name

7name

8name

9name