猿问

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

==== 根对象 ====


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 一样作为响应,我应该以大写或小开头


回首忆惘然
浏览 109回答 1
1回答

慕哥6287543

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 {&nbsp; &nbsp; private Map<String, InventoryDetail> inventoryDetail = new HashMap<>();&nbsp; &nbsp; public Map<String, InventoryDetail> getInventoryDetail() {&nbsp; &nbsp; &nbsp; &nbsp; return inventoryDetail;&nbsp; &nbsp; }&nbsp; &nbsp; public void setInventoryDetail(Map<String, InventoryDetail> inventoryDetail) {&nbsp; &nbsp; &nbsp; &nbsp; this.inventoryDetail = inventoryDetail;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(JsonGenerator arg0, SerializerProvider arg1) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; arg0.writeObject(inventoryDetail);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {&nbsp; &nbsp; }}对于你的最后一个问题:@JsonTypeName("InventoryDetail") // any name@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT ,use = JsonTypeInfo.Id.NAME)&nbsp;&nbsp; &nbsp; `
随时随地看视频慕课网APP

相关分类

Java
我要回答