Loading

多层嵌套json转bean(属性包含List<t>)

  //指定泛型类型
  Map<String, Class> classMap = new HashMap<>(1);
  classMap.put("list", WaterPressureHistoryData.class);
  
  // WaterPressureHistoryData中包含date类型,json中传输的是时间戳,
  JSONUtils.getMorpherRegistry().registerMorpher(new TimestampToDateMorpher());
  rRespDTO = (PageDataRespDTO<WaterPressureHistoryData>) JSONObject.toBean(JSONObject.fromObject(pResp.getData()), PageDataRespDTO.class,classMap);
public class TimestampToDateMorpher extends AbstractObjectMorpher {

    @Override
    public Object morph(Object value) {
        if( value != null){
            return new Date(Long.parseLong(String.valueOf(value)));
        }
        return null;
    }

    @Override
    public Class morphsTo() {
        return Date.class;
    }

    @Override
    public boolean supports( Class clazz ){
        return Long.class.isAssignableFrom( clazz );
    }
}

public class WaterPressureHistoryData {

    @ApiModelProperty(value = "水表编号")
    private String code;


    @ApiModelProperty(value = "采集时间")
    private Date collectTime;

    @ApiModelProperty(value = "计量电压")
    private Double meterVoltage;

    @ApiModelProperty(value = "通讯电压")
    private Double collectVoltage;

    @ApiModelProperty(value = "水表压力")
    private Double pressure;

}
public class PageDataRespDTO<T> {

    private List<T> list;
    private String total;
}
posted @ 2022-08-29 14:41  半瓶牛奶🥛  阅读(190)  评论(0编辑  收藏  举报