首先,创建个实体类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;
        this.gender = gender;
    }

    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) {
        this.gender = gender;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }
}

 

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.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 = ZonedDateTime.now(ZoneId.of("GMT+8"));
        System.out.println("start: " + start.toString());
        //关键:使用 objectMapper 的 readValue(String content, TypeReference<T> valueTypeRef) 方法
        List<Person> personList1 = objectMapper.readValue(personListJson, new TypeReference<List<Person>>() {});
        ZonedDateTime end = ZonedDateTime.now(ZoneId.of("GMT+8"));
        System.out.println("end: " + end.toString());
        long between = ChronoUnit.MILLIS.between(start, end);
        //方式1比较简单,但是耗时较多,是方式2的几十倍
        System.out.println("between: " + between);



        //将数组json转成对象集合,方式2:
        start = ZonedDateTime.now(ZoneId.of("GMT+8"));
        System.out.println("start: " + start.toString());
        //关键:使用 objectMapper 的 readValue(String content, JavaType valueType) 方法
        //注意:此方法返回的是Object,特此这里用了类型强转为List
        List o = (List) objectMapper.readValue(personListJson, objectMapper.getTypeFactory().constructParametricType(List.class, Person.class));
        end = ZonedDateTime.now(ZoneId.of("GMT+8"));
        System.out.println("end: " + end.toString());
        long until = start.until(end, ChronoUnit.MILLIS);
        //方式2略复杂,但是耗时较少
        System.out.println("until: " + until);
    }

}

 

 

 

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:14 天前 )
960b763e 3 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐