为了账号安全,请及时绑定邮箱和手机立即绑定

反序列化 Pojo 时如何忽略 JSON 根元素

反序列化 Pojo 时如何忽略 JSON 根元素

回首忆惘然 2023-07-19 15:49:09
==== 根对象 ====import java.util.HashMap;import java.util.Map;import java.util.Objects;public class RoomInventoryResponse {    private Map<String, InventoryDetail> inventoryDetail = new HashMap<>();    public Map<String, InventoryDetail> getInventoryDetail() {        return inventoryDetail;    }    public void setInventoryDetail(Map<String, InventoryDetail> inventoryDetail) {        this.inventoryDetail = inventoryDetail;    }   // To String overridden}==== 库存明细对象 ====import java.util.Objects;public class InventoryDetail {    private Integer booked;    private Integer available;    public Integer getBooked() {        return booked;    }    public void setBooked(Integer booked) {        this.booked = booked;    }    public Integer getAvailable() {        return available;    }    public void setAvailable(Integer available) {        this.available = available;    }   // To String overridden}====== 运行mentod ====public class Test{    public static void main(String[] args) throws JsonProcessingException     ObjectWriter ow = new  ObjectMapper().writer().withDefaultPrettyPrinter();            String json = ow.writeValueAsString(request);            RoomInventoryResponse response = new RoomInventoryResponse();            Map<String, InventoryDetail> map = new HashMap<>();            InventoryDetail inventoryDetail = new InventoryDetail();            inventoryDetail.setAvailable(10);            inventoryDetail.setBooked(10);            map.put("2019-02-21", inventoryDetail);            InventoryDetail inventoryDetail2 = new InventoryDetail();            inventoryDetail2.setAvailable(15);            inventoryDetail2.setBooked(10);            map.put("2019-02-22", inventoryDetail2);       }    }另一个要求是如何更改大写或小为 inventoryDetail 的根元素标签,使之像 InventoryDetail 一样作为响应,我应该以大写或小开头
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.JsonSerializable;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.jsontype.TypeSerializer;


import java.io.IOException;

import java.util.HashMap;

import java.util.Map;


public class RoomInventoryResponse implements JsonSerializable {



    private Map<String, InventoryDetail> inventoryDetail = new HashMap<>();


    public Map<String, InventoryDetail> getInventoryDetail() {

        return inventoryDetail;

    }


    public void setInventoryDetail(Map<String, InventoryDetail> inventoryDetail) {

        this.inventoryDetail = inventoryDetail;

    }


    @Override

    public void serialize(JsonGenerator arg0, SerializerProvider arg1) throws IOException {

        arg0.writeObject(inventoryDetail);

    }


    @Override

    public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {


    }

}

对于你的最后一个问题:


@JsonTypeName("InventoryDetail") // any name

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME) 

    `


查看完整回答
反对 回复 2023-07-19
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信