对象转JSON正则查找替换

package com.smil.gws.wbd.labor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smil.gws.common.utils.GwsReportUtils;
import com.smil.gws.report.vo.WcmWarrantyClaimExportView;
import org.bouncycastle.util.StringList;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {
public static void main(String[] args) {
WcmWarrantyClaimExportView view = new WcmWarrantyClaimExportView();
view.setStatus("10");
view.setClaimType("2");
view.setAdjustAdditionalLaborTimes(null);
view.setSubmitDate("2020-01-12");
view.setApproveDate("2020-02-12");
view.setProductionDate("2020-03-12");
view.setPurchaseDate("2020-03-12");
view.setReceptionDate("2020-04-12");
view.setClaimNo("我是-订单号");
String ysj = JSON.toJSON(view).toString();
System.out.println(ysj);
String pat = "\d{4}-\d{2}-\d{2}";
Pattern pattern = Pattern.compile(pat);
Matcher matcher = pattern.matcher(ysj);
List<String> stringList = new ArrayList<>();
while(matcher.find()){
stringList.add(matcher.group());
}
for (String s : stringList) {
if (ysj.contains(s)){
ysj = ysj.replace("Date":""+s,"Date":""+s.replace("-","/"));
}
}
WcmWarrantyClaimExportView wcmWarrantyClaimExportView = JSONObject.toJavaObject(JSON.parseObject(ysj), WcmWarrantyClaimExportView.class);
System.out.println(JSON.toJSON(wcmWarrantyClaimExportView).toString());
}
}
原文地址:https://www.cnblogs.com/jabez1992/p/14024043.html