• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Jackson库中objectMapper的用法

java 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论
文章目录[隐藏]

Jackson库中objectMapper用法

ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象与SON结构互相转换,在项目中遇到过,故记录一下。

在 pom.xml 加入依赖

<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.8.3</version>
 </dependency>

创建一个实体类RiemannUser:

package com.test.objectMapper;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
 * @author riemann
 * @date 2019/05/27 22:48
 */
public class RiemannUser implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String message;
    private Date sendDate;
    private String nodeName;
    private List<Integer> intList;
    public RiemannUser() {
        super();
    }
    public RiemannUser(int id, String message, Date sendDate) {
        super();
        this.id = id;
        this.message = message;
        this.sendDate = sendDate;
    }
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Date getSendDate() {
        return sendDate;
    }
    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }
    public String getNodeName() {
        return nodeName;
    }
    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }
    public List<Integer> getIntList() {
        return intList;
    }
    public void setIntList(List<Integer> intList) {
        this.intList = intList;
    }
    @Override
    public String toString() {
        return "RiemannUser{" + "id=" + id + ", message='" + message + '\'' + ", sendDate=" + sendDate + ", nodeName='" + nodeName + '\'' + ", intList=" + intList + '}';
    }
}

先创建一个ObjectMapper,然后赋值一些属性:

public static ObjectMapper mapper = new ObjectMapper();
static {
    // 转换为格式化的json
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    // 如果json中有新增的字段并且是实体类类中不存在的,不报错
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

1、对象与json字符串、byte数组

@Test
public void testObject() throws JsonGenerationException, JsonMappingException, IOException {
    RiemannUser riemann = new RiemannUser(1,"Hello World", new Date());
    mapper.writeValue(new File("D:/test.txt"), riemann);//写到文件中
    //mapper.writeValue(System.out, riemann); //写到控制台
    String jsonStr = mapper.writeValueAsString(riemann);
    System.out.println("对象转json字符串: " + jsonStr);
    byte[] byteArr = mapper.writeValueAsBytes(riemann);
    System.out.println("对象转为byte数组:" + byteArr);
    RiemannUser riemannUser = mapper.readValue(jsonStr, RiemannUser.class);
    System.out.println("json字符串转为对象:" + riemannUser);
    RiemannUser riemannUser2 = mapper.readValue(byteArr, RiemannUser.class);
    System.out.println("byte数组转为对象:" + riemannUser2);
}

运行结果:

对象转json字符串: {
“id” : 1,
“message” : “Hello World”,
“sendDate” : 1558971056693,
“nodeName” : null,
“intList” : null
}
对象转为byte数组:[B@31610302
json字符串转为对象:RiemannUser{id=1, message=’Hello World’, sendDate=Mon May 27 23本文来源gao*daima.com搞@代#码&网6:30:56 CST 2019, nodeName=’null’, intList=null}
byte数组转为对象:RiemannUser{id=1, message=’Hello World’, sendDate=Mon May 27 23:30:56 CST 2019, nodeName=’null’, intList=null}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Jackson库中objectMapper的用法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址