javaBean
和json
的互转JavaBean
类
public class Person {
private Integer id;
private String name;
public Person() {
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
JsonTest
测试类
@Test
public void test1(){
Person person = new Person(1, "张三");
Gson gson = new Gson();
String personJsonToString = gson.toJson(person); //toJson()方法可以把java对象转换成json字符串
System.out.println(personJsonToString);
Person person1 = gson.fromJson(personJsonToString, Person.class);//fromJson()方法可以把json字符串转换成java对象
System.out.println(person1);
}
List
和 json
的互转public class PersonListType extends TypeToken<ArrayList<Person>> {
}
@Test
public void test2(){
ArrayList<Person> personArrayList = new ArrayList<>();
personArrayList.add(new Person(1,"张三"));
personArrayList.add(new Person(2,"李四"));
personArrayList.add(new Person(3,"王五"));
Gson gson = new Gson();
//把List转换为json字符串
String personListToString = gson.toJson(personArrayList);
System.out.println(personListToString);
//把json字符串祝转化成List
List<Person> list = gson.fromJson(personListToString, new PersonListType().getType());
System.out.println(list);
Person person = list.get(0);
System.out.println(person);
}
输出结果:
如果是直接personArrayList.getClass()
,将json
字符串转化成List
,然后取List
第一个元素进行输出,会出现异常
//List list = gson.fromJson(personListToString, new PersonListType().getType());
List<Person> list = gson.fromJson(personListToString, personArrayList.getClass());
System.out.println(list);
System.out.println(list.get(0) instanceof Person);
System.out.println(list.get(0) instanceof List);
System.out.println(list.get(0) instanceof Map);
Person person = list.get(0);
System.out.println(person);
从结果可以看出由json
转换来的List
里面的元素并不是一个Person
对象,也不是一个List
对象,而是一个Map
对象,所以强转Person
对象会报错。
如果要不把json
字符串转换成List
集合,并且集合中的每一个元素都是Person
类型对象,第二个参数必须使用Type
类型。
新建一个PersonListType
类,继承于TypeToken
类
//TypeToken是泛型类,List、或者具体的ArrayList都可以
public class PersonListType extends TypeToken<ArrayList<Person>> {
}
List<Person> list = gson.fromJson(personListToString, new PersonListType().getType());
//List list = gson.fromJson(personListToString, personArrayList.getClass());
System.out.println(list);
System.out.println(list.get(0) instanceof Person);
System.out.println(list.get(0) instanceof List);
System.out.println(list.get(0) instanceof Map);
Person person = list.get(0);
System.out.println(person);
map
和 json
的互转PersonMapType
类,有个返回JavaBean
可以直接.getClass()
,而如果返回是一个集合对象,则需要继承TypeToken
类。
public class PersonMapType extends TypeToken<HashMap<Integer, Person>>{
}
@Test
public void test3(){
Map<Integer,Person> map = new HashMap<Integer,Person>();
map.put(1,new Person(1,"张三"));
map.put(2,new Person(2,"李四"));
Gson gson = new Gson();
//把map集合转换成为json字符串
String personMapJsonString = gson.toJson(map);
System.out.println(personMapJsonString);
//把json字符串转换成map集合
Map<Integer,Person> personMap1 = gson.fromJson(personMapJsonString, new PersonMapType().getType());
System.out.println(personMap1);
Person person = personMap1.get(1);
System.out.println(person);
}