当前位置:   article > 正文

Jackson:数组json字符串转对象集合(List)的两种方式_jackson字符串转换为list

jackson字符串转换为list

首先,创建个实体类Person

  1. import java.util.List;
  2. public class Person {
  3. private String name;
  4. private Integer age;
  5. private Gender gender;
  6. private List<String> hobbies;
  7. public enum Gender {
  8. MALE("male"),
  9. FEMALE("female");
  10. private String desc;
  11. Gender(String desc) {
  12. this.desc = desc;
  13. }
  14. }
  15. public Person() {
  16. }
  17. public Person(String name) {
  18. this.name = name;
  19. }
  20. public Person(String name, Integer age) {
  21. this.name = name;
  22. this.age = age;
  23. }
  24. public Person(String name, Integer age, Gender gender) {
  25. this.name = name;
  26. this.age = age;
  27. this.gender = gender;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public Integer getAge() {
  36. return age;
  37. }
  38. public void setAge(Integer age) {
  39. this.age = age;
  40. }
  41. public Gender getGender() {
  42. return gender;
  43. }
  44. public void setGender(Gender gender) {
  45. this.gender = gender;
  46. }
  47. public List<String> getHobbies() {
  48. return hobbies;
  49. }
  50. public void setHobbies(List<String> hobbies) {
  51. this.hobbies = hobbies;
  52. }
  53. }

 

下面开始创建对象集合personList,先用ObjectMapper将对象集合读成数组json字符串,然后用ObjectMapper将数组json字符串转回对象集合。

  1. import com.fasterxml.jackson.core.JsonProcessingException;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6. import java.time.temporal.ChronoUnit;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class JsonDemo {
  10. public static void main(String[] args) throws JsonProcessingException {
  11. //创建对象集合,添加元素
  12. List<Person> personList = new ArrayList<>();
  13. personList.add(new Person("Tom", 18, Person.Gender.MALE));
  14. personList.add(new Person("Jim", 19, Person.Gender.MALE));
  15. personList.add(new Person("John", 18, Person.Gender.MALE));
  16. personList.add(new Person("Jacky", 19, Person.Gender.MALE));
  17. personList.add(new Person("Lily", 17, Person.Gender.FEMALE));
  18. personList.add(new Person("Lucy", 17, Person.Gender.FEMALE));
  19. personList.add(new Person("Rose", 18, Person.Gender.FEMALE));
  20. personList.add(new Person("Nancy", 18, Person.Gender.FEMALE));
  21. //循环遍历,往集合里继续添加元素
  22. for (int i = 0; i < 5; i++) {
  23. personList.addAll(personList);
  24. }
  25. //打印集合的元素个数
  26. System.out.println("personList.size: " + personList.size());
  27. ObjectMapper objectMapper = new ObjectMapper();
  28. //将对象集合读成数组json
  29. String personListJson = objectMapper.writeValueAsString(personList);
  30. //将数组json转成对象集合,方式1:
  31. ZonedDateTime start = ZonedDateTime.now(ZoneId.of("GMT+8"));
  32. System.out.println("start: " + start.toString());
  33. //关键:使用 objectMapper 的 readValue(String content, TypeReference<T> valueTypeRef) 方法
  34. List<Person> personList1 = objectMapper.readValue(personListJson, new TypeReference<List<Person>>() {});
  35. ZonedDateTime end = ZonedDateTime.now(ZoneId.of("GMT+8"));
  36. System.out.println("end: " + end.toString());
  37. long between = ChronoUnit.MILLIS.between(start, end);
  38. //方式1比较简单,但是耗时较多,是方式2的几十倍
  39. System.out.println("between: " + between);
  40. //将数组json转成对象集合,方式2:
  41. start = ZonedDateTime.now(ZoneId.of("GMT+8"));
  42. System.out.println("start: " + start.toString());
  43. //关键:使用 objectMapper 的 readValue(String content, JavaType valueType) 方法
  44. //注意:此方法返回的是Object,特此这里用了类型强转为List
  45. List o = (List) objectMapper.readValue(personListJson, objectMapper.getTypeFactory().constructParametricType(List.class, Person.class));
  46. end = ZonedDateTime.now(ZoneId.of("GMT+8"));
  47. System.out.println("end: " + end.toString());
  48. long until = start.until(end, ChronoUnit.MILLIS);
  49. //方式2略复杂,但是耗时较少
  50. System.out.println("until: " + until);
  51. }
  52. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/485526
推荐阅读
相关标签
  

闽ICP备14008679号