jackson json对象、实体类相互转换

2023年5月5日14:08:18

json对象转换为实体类,使用jackson 进行jsonObject、实体类对象转换。

首先导入依赖

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

常用方法有如下几个:readValue、writer、writeValueAsString

 public <T> T readValue(File src, Class<T> valueType) throws IOException, JsonParseException, JsonMappingException {
        return this._readMapAndClose(this._jsonFactory.createParser(src), this._typeFactory.constructType(valueType));
    }
    public ObjectWriter writer() {
        return this._newWriter(this.getSerializationConfig());
    }

 public String writeValueAsString(Object value) throws JsonProcessingException {
        SegmentedStringWriter sw = new SegmentedStringWriter(this._jsonFactory._getBufferRecycler());

        try {
            this._configAndWriteValue(this._jsonFactory.createGenerator(sw), value);
        } catch (JsonProcessingException var4) {
            throw var4;
        } catch (IOException var5) {
            throw JsonMappingException.fromUnexpectedIOE(var5);
        }

        return sw.getAndClear();
    }

  • 作者:凝步
  • 原文链接:https://blog.csdn.net/weixin_43143345/article/details/103510832
    更新时间:2023年5月5日14:08:18 ,共 905 字。