Jackson:数组json字符串转对象集合(List)的两种⽅式⾸先,创建个实体类Person
import java.util.List;
public class Person {
private String name;
private Integer age;
private Gender gender;
private List<String> hobbies;
public enum Gender {
MALE("male"),
FEMALE("female");
private String desc;
Gender(String desc) {
this.desc = desc;
}
}
public Person() {
}
public Person(String name) {
this.name = name;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person(String name, Integer age, Gender gender) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
}
下⾯开始创建对象集合personList,先⽤ObjectMapper将对象集合读成数组json字符串,然后⽤ObjectMapper将数组json字符串转回对象集合。
import com.JsonProcessingException;
import com.ype.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import poral.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
public class JsonDemo {
public static void main(String[] args) throws JsonProcessingException {
//创建对象集合,添加元素
List<Person> personList = new ArrayList<>();
personList.add(new Person("Tom", 18, Person.Gender.MALE));
personList.add(new Person("Jim", 19, Person.Gender.MALE));
personList.add(new Person("John", 18, Person.Gender.MALE));
personList.add(new Person("Jacky", 19, Person.Gender.MALE));
personList.add(new Person("Lily", 17, Person.Gender.FEMALE));
personList.add(new Person("Lucy", 17, Person.Gender.FEMALE));
personList.add(new Person("Rose", 18, Person.Gender.FEMALE));
personList.add(new Person("Nancy", 18, Person.Gender.FEMALE));
//循环遍历,往集合⾥继续添加元素
for (int i = 0; i < 5; i++) {
personList.addAll(personList);
}
//打印集合的元素个数
System.out.println("personList.size: " + personList.size());
ObjectMapper objectMapper = new ObjectMapper();
//将对象集合读成数组json
String personListJson = objectMapper.writeValueAsString(personList);
//将数组json转成对象集合,⽅式1:
ZonedDateTime start = w(ZoneId.of("GMT+8"));
System.out.println("start: " + String());
/
/关键:使⽤ objectMapper 的 readValue(String content, TypeReference<T> valueTypeRef) ⽅法
json转换对象List<Person> personList1 = adValue(personListJson, new TypeReference<List<Person>>() {});
ZonedDateTime end = w(ZoneId.of("GMT+8"));
System.out.println("end: " + String());
System.out.println("end: " + String());
long between = ChronoUnit.MILLIS.between(start, end);
//⽅式1⽐较简单,但是耗时较多,是⽅式2的⼏⼗倍
System.out.println("between: " + between);
//将数组json转成对象集合,⽅式2:
start = w(ZoneId.of("GMT+8"));
System.out.println("start: " + String());
//关键:使⽤ objectMapper 的 readValue(String content, JavaType valueType) ⽅法
//注意:此⽅法返回的是Object,特此这⾥⽤了类型强转为List
List o = (List) adValue(personListJson, TypeFactory().constructParametricType(List.class, Person.class));        end = w(ZoneId.of("GMT+8"));
System.out.println("end: " + String());
long until = start.until(end, ChronoUnit.MILLIS);
//⽅式2略复杂,但是耗时较少
System.out.println("until: " + until);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。