NutzCN Logo
问答 返回json格式 不忽略null字段
发布于 2915天前 作者 king666 4987 次浏览 复制 上一个帖子 下一个帖子
标签: json

如题,一个实体的某个字段如果为null,在转json后,没有这个属性,需要有什么设置可以显示空数据字段

12 回复
@Ok("json:full")

@wendal 是加到对应字段还是实体,还是哪?

@qq_640d75e3 说的不是入口方法的返回吗?

@wendal 例如Jackson的@JsonInclude

实体没有这种配置

Json.toJson方法的话

Json.toJson(obj, JsonFormat.full());

@wendal 个人觉得nutz需要关注一下https://nutz.cn/yvr/t/7fmfpb9oguivrqck7add0u7i1r这个问题

我添加了@Ok("json:full")之后null字段还是没有显示

@IocBean
@Ok("json:full")
@Fail("http:500")
@At("/facManageCar")
public class FacManagerCarModule extends BaseModule{

@At
public Object queryFacManageCar(FacManageCarReq req){ 
    FacManageCarResp resp = new FacManageCarResp();
    Pager pager = dao.createPager(req.page,20);
    Cnd where = Cnd.NEW();
    where.and("facId","=",req.factoryId);   
    if(req.carStatus==1){
       where.and("carStatus","=",false);
    }else if(req.carStatus==2){
       where.and("carStatus","=",true);
    }
    //车牌号
    if(Strings.isNotBlank(req.carLicence)){
       where.and("carLicence","like","%"+req.carLicence+"%");
    }
    //车主手机号
    if(Strings.isNotBlank(req.cellPhone)){
       where.and("cellPhone","like","%"+req.cellPhone+"%");
    }
    //车主姓名
    if(Strings.isNotBlank(req.realName)){
       where.and("realName","like","%"+req.realName+"%");
    }
    //保养到期
    if(req.carNextTime!=null){
       where.and("carNextTime","<=",req.carNextTime);
    }
    //保险到期
    if(req.insuranceEndDate!=null){       
       where.and("insuranceEndDate","<=",req.insuranceEndDate);            
    }
    //年检到期
    if(req.checkEndDate!=null){
       where.and("checkEndDate","<=",req.checkEndDate);
    }   
    //根据字段名字排序
    if (req.sortType == null) {
       where.orderBy("createTime", "desc");
    } else {
       where.orderBy("convert (" + req.sortType.getName() + " using gbk)",
          "" + req.sortType.getSorts() + "");
    }   
    List <FacManageCarView> fmcvs =  dao.query(FacManageCarView.class, where,pager);

    int count =dao.count(FacManageCarView.class,where);
    if(fmcvs==null||fmcvs.size()==0){
        resp.requestCode=-1;
        return resp;
    }
    List<FacManageCarEntity> fmces = new ArrayList<FacManageCarEntity>();
    List<Dictionary> dcs = dao.query(Dictionary.class,Cnd.where("type","=","car_state"));
    for(int i=0;i<fmcvs.size();i++){
       FacManageCarEntity fmce = new FacManageCarEntity();     
       fmce.SNumber = (req.page-1)*20+i+1;//序号赋值
       int code;
       if(fmcvs.get(i).isCarStatus()==false){
         code = 1;
       }else{
         code = 2;
       }
       for(int j=0;j<dcs.size();j++){
         if(dcs.get(j).getVal().equals(""+code)){
          fmce.carStatusDesc = dcs.get(j).getItems();//车辆状态描述
          break;
         }
       }
       fmce.fmcv = fmcvs.get(i);
       fmces.add(fmce);
    }   
    pager.setRecordCount(count);
    resp.pageCount = pager.getPageCount();
    resp.count = count;
    resp.fmces = fmces;
    return resp;          
}
@At
public Object fetchFacManageCar(FacManageCarReq req){
    FacManageCarResp resp = new FacManageCarResp();
    //车辆id
    if(req.Id<=0){
       resp.requestCode=-1;
       return resp;
    }
    FacManageCarView fmcv = dao.fetch(FacManageCarView.class,Cnd.where("id","=",req.Id));
    if(fmcv==null){
       resp.requestCode=-1;
       return resp;
    }
    FacManageCarEntity fmce = new FacManageCarEntity();
    fmce.fmcv = fmcv;
    int code;
    if(fmcv.isCarStatus()==false){
       code = 1;
    }else{
       code = 2;
    }
    Dictionary dc = dao.fetch(Dictionary.class,Cnd.where("type","=","car_state").and("val","=",code));
    fmce.carStatusDesc = dc.getItems();//车辆使用状态描述
    resp.requestCode=0;
    resp.fmce = fmce;
    return resp;
}
@At
public Object queryFacManageRecord(FacManageCarReq req){
    FacManageCarResp resp = new FacManageCarResp();
    Pager pager = dao.createPager(req.page, 20);
    if(req.carId<=0){
       resp.requestCode = -1;
       return resp;
    }
    Cnd where  = Cnd.where("carId","=",req.carId);
    if (req.sortType == null) {
       where.orderBy("createTime", "desc");
    } else {
       where.orderBy("convert (" + req.sortType.getName() + " using gbk)",
          "" + req.sortType.getSorts() + "");
    }
    List<RepairRecord> rrs = dao.query(RepairRecord.class,where,pager);
    if(rrs==null||rrs.size()==0){
       resp.requestCode =-1;
       return resp;
    }
    int count  = dao.count(RepairRecord.class,where);
    pager.setRecordCount(count);
    List<FacManageRepairRecordEntity> fmrres = new ArrayList<FacManageRepairRecordEntity>();
    List<Dictionary> dr = dao.query(Dictionary.class,Cnd.where("type","=","repairrecord_type"));
    for(int i=0;i<rrs.size();i++){
       FacManageRepairRecordEntity fmrre = new FacManageRepairRecordEntity();
       fmrre.rr = rrs.get(i);
       fmrre.SNmuber = (req.page-1)*20+i+1;
       for(Dictionary d : dr){
         if(d.getVal().equals(""+rrs.get(i).getType()+1)){
          fmrre.typeDesc = d.getItems();//维修保养记录类型描述
         }
       }
       fmrres.add(fmrre);
    }
    resp.requestCode=0;
    resp.fmrres = fmrres;
    resp.count = count;
    resp.pageCount = pager.getPageCount();
    return resp;

}
/**
 * 维修保养项目接口
 * @author fjd 
 * @param req
 * @return
 */
@At
public Object queryFacManageOrderItme(FacManageOrderItmeReq req){
    FacManageOrderItmeResp resp = new FacManageOrderItmeResp();
    if(req.id<=0){
       resp.requestCode = -1;
       return resp;
    }
    List<OrderItme> ois = dao.query(OrderItme.class, Cnd.where("rrId","=",req.id));       
    if(ois==null||ois.size()==0){
       resp.requestCode = -1;
       return resp;
    }
    int count  =  dao.count(OrderItme.class,Cnd.where("rrId","=",req.id));
    List<FacManageOrderItmeEntity> fmoies = new ArrayList<FacManageOrderItmeEntity>();
    List<Dictionary> dlist = dao.query(Dictionary.class,Cnd.where("type","=","repairrecord_type"));
    for(int i=0;i<ois.size();i++){
       FacManageOrderItmeEntity fmoie = new FacManageOrderItmeEntity();
       fmoie.oi = ois.get(i);
       fmoie.SNumber = (req.page-1)*20+i+1;//序号
       for(Dictionary d : dlist){
         if(d.getVal().equals(""+ois.get(i).getType())){
          fmoie.typeDesc = d.getItems();//维修保养记录描述
         }
       }
       fmoies.add(fmoie);
    }
    resp.count = count;
    resp.fmoies = fmoies;
    return resp;
}

}

好复杂, 先弄个demo试吧

@Ok("json:full") 怎么将 null 默认转成 空串

添加回复
请先登陆
回到顶部