JSON数据转换

目录

对象-->JSON

List-->JSON

Map-->JSON

JSON-->对象:

JSON-->List

JSON-->Map

日期类型


JSON是一种轻量级的数据格式,全称为JavaScript Object Notation

1.首先导入jar包

2.创建一个实体类

3.

对象-->JSON

//对象 ->json
    @Test
    public void test01() throws JsonProcessingException {
        //1.1创建对象
        Persion persion = new Persion("孙悟空", 1000, "男");
        //2.创建jackson
        ObjectMapper mapper = new ObjectMapper();
        //3.调用方法完成转换
        String s = mapper.writeValueAsString(persion);
        System.out.println("s = " + s);//{"name":"孙悟空","age":1000,"gender":"男"}
    }

List-->JSON

//list集合->json
    @Test
    public void test02() throws JsonProcessingException {
        ArrayList<Persion> list = new ArrayList<>();
        Collections.addAll(list,new Persion("孙悟空", 1000, "男"),
                 new Persion("沙悟净", 1000, "男"));
        //2.创建jackson
        ObjectMapper mapper = new ObjectMapper();
        //3.调用方法完成转换
        String s = mapper.writeValueAsString(list);
        System.out.println("s = " + s);
    }

Map-->JSON

//map -> JSON
    @Test
    public void test03() throws JsonProcessingException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","孙悟空");
        map.put("age",1222);
        map.put("gender","男");

        //2.创建jackson
        ObjectMapper mapper = new ObjectMapper();
        //3.调用方法完成转换
        String s = mapper.writeValueAsString(map);
        System.out.println("s = " + s);
    }

JSON-->对象:

//JSON->对象
    @Test
    public void test01() throws IOException {
        String s = "{\"name\":\"孙悟空\",\"age\":1000,\"gender\":\"男\"}";

        ObjectMapper mapper = new ObjectMapper();
        //第一个参数:json字符串,第二个参数:要转的类型
        //为什么要.class,因为源码的第二个参数是class类型
        Persion persion = mapper.readValue(s, Persion.class);
        System.out.println(persion);
    }

JSON-->List

 //JSON->list集合
    @Test
    public void test02() throws IOException {
        String s = "[{\"name\":\"孙悟空\",\"age\":1000,\"gender\":\"男\"}," +
                "{\"name\":\"孙悟空\",\"age\":1000,\"gender\":\"男\"}]";
        ObjectMapper mapper = new ObjectMapper();
        ArrayList arrayList = mapper.readValue(s, ArrayList.class);
        System.out.println("arrayList = " + arrayList);
    }

JSON-->Map

@Test
    public void test03() throws IOException {
        String s = "{\"name\":\"孙悟空\",\"age\":1000,\"gender\":\"男\"}";
        ObjectMapper mapper = new ObjectMapper();
        HashMap map = mapper.readValue(s, HashMap.class);
        System.out.println(map);
    }

日期类型

如果在实体类中有日期类类型,转换json格式我们可以自定义格式

@Test
    public void test01() throws JsonProcessingException {
        Student student = new Student("tom", new Date());

        ObjectMapper mapper = new ObjectMapper();
        String string = mapper.writeValueAsString(student);
        System.out.println("string = " + string);
        //在日期类型变为毫秒值
        //在日期字段加上注解:@JsonFormat(pattern = "yyyy-MM-dd")定义格式,

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值