JackJson - JSON to Java

1 篇文章 0 订阅
public class People implements Serializable{
        private Long id;
        private String name;
        private Integer age;

        public People() {
        }

        public People(Long id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        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;
        }

        @Override
        public String toString() {
            return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
        }
    }

 

public class A<E,F>{
        private E e;
        private F f;

        public F getF() {
            return f;
        }

        public void setF(F f) {
            this.f = f;
        }

        public E getE() {
            return e;
        }

        public void setE(E e) {
            this.e = e;
        }
    }

 

 

public class JsonTest {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper(); // create once, reuse
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
            .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        TypeFactory typeFactory = mapper.getTypeFactory();

        //json to pojo
        String jsonSource = "{id:1,name:'liyuan',age:20}";
        People people = mapper.readValue(jsonSource, People.class);  //recommend
        people = mapper.readValue(jsonSource,new TypeReference<People>(){});
        people = mapper.readValue(jsonSource, typeFactory.constructType((People.class)));

        //json to map
        Map<String,Object> map = mapper.readValue(jsonSource,new TypeReference<HashMap<String,Object>>(){});  //recommend
        System.out.println(mapper.writeValueAsString(map));
        map = mapper.readValue(jsonSource, typeFactory.constructMapType(HashMap.class, String.class, Object.class));
        System.out.println(mapper.writeValueAsString(map));

        //json to node
        JsonNode root = mapper.readTree(jsonSource);
        System.out.println(root.get("id").asLong());
        System.out.println(root.get("name").asText());

        //json to array
        jsonSource = "[{id:1,name:'liyuan',age:20},{id:2,name:'xiaoming',age:15}]";
        People[] arr = mapper.readValue(jsonSource, new TypeReference<People[]>(){});
        System.out.println(mapper.writeValueAsString(arr));
        arr = mapper.readValue(jsonSource, typeFactory.constructArrayType(People.class));
        System.out.println(mapper.writeValueAsString(arr));

        //json to list
        List<People> list = mapper.readValue(jsonSource, new TypeReference<List<People>>(){});
        System.out.println(mapper.writeValueAsString(list));
        list = mapper.readValue(jsonSource, typeFactory.constructCollectionType(ArrayList.class,People.class));
        System.out.println(mapper.writeValueAsString(list));

        //json to generic
        jsonSource = "{e:{id:1,name:'liyuan',age:20},f:{id:2,name:'xiaoming',age:15}}";
        A<People,People> generic = mapper.readValue(jsonSource, new TypeReference<A<People,People>>(){});
        System.out.println(mapper.writeValueAsString(generic));
        generic = mapper.readValue(jsonSource, typeFactory.constructParametricType(A.class,People.class,People.class));
        System.out.println(mapper.writeValueAsString(generic));
    }
}

 

<audio controls="controls" style="display: none;"></audio>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值