JackJson - JSON to Java

5 篇文章 0 订阅
public class JsonTest {  
    public static void main(String[] args) throws Exception {  
        // create once, reuse
        ObjectMapper mapper = new ObjectMapper();   
        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));  
    }  
}  
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;  
        }  
        //省略setter和getter方法
    } 
public class A<E,F>{  
        private E e;  
        private F f;  
        //省略setter和getter方法
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值