JSON--解析器Jackson--java转json

这篇博客演示了如何使用Jackson库的ObjectMapper将Java对象(如User类)转换为JSON字符串,并将结果写入文件。还展示了如何处理日期格式化以及将List和Map转换为JSON。

黑马程序员

JSON–解析器Jackson–java转json

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package cn.itcast.web.json;

import cn.itcast.web.domain.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class JsonTest {

    @Test
    public void test1() throws IOException {
        User user = new User("张三",12,new Date());

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);
        
        mapper.writeValue(new File("F:\\a.txt"),user);

        System.out.println(s);

    }
}

{"name":"张三","age":12,"birthday":1602146360562}

在这里插入图片描述

jar包
在这里插入图片描述

JSON–解析器Jackson–java转json–注解

在这里插入图片描述

在这里插入图片描述

package cn.itcast.web.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private String name;
    private int age;
    //@JsonIgnore//转化为JSON时忽略此属性
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;

    public User() {
    }

//    public String getBirStr(){
//        if (this.birthday!=null)
//            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.birthday);
//        return "";
//    }

    public User(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

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

package cn.itcast.web.json;

import cn.itcast.web.domain.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class JsonTest {

    @Test
    public void test1() throws IOException {
        User user = new User("张三",12,new Date());

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);

        mapper.writeValue(new File("F:\\a.txt"),user);

        System.out.println(s);

    }
}

JSON–解析器Jackson–java转json–List&Map

    @Test
    public void test2() throws IOException {
        List<User> users = new ArrayList<>();
        users.add(new User("张三", 12, new Date()));
        users.add(new User("李四", 13, new Date()));
        users.add(new User("王五", 14, new Date()));

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(users);

        //mapper.writeValue(new File("F:\\a.txt"),user);

        System.out.println(s);

    }
    @Test
    public void test3() throws IOException {
        HashMap<String, Object> usersMap = new HashMap<>();
        usersMap.put("name","张三");
        usersMap.put("age",12);
        usersMap.put("birthday",new Date());

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(usersMap);

        //mapper.writeValue(new File("F:\\a.txt"),user);

        System.out.println(s);

    }

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值