1.json 字符串转换成对象
SyncCarriageStatusDTO dto= JSON.parseObject(value,SyncCarriageStatusDTO.class);
List<MQPackageParams> pparams = JSON.parseObject(dto.getData(),new TypeReference<List<MQPackageParams>>() {});
2.对象转换成字符串对象
String data = JSON.toJSONString(pkg);
2.枚举字符串转换成枚举类型
RefTypeEnum refType=RefTypeEnum.valueof(dto.getRefType())
3.对象不为空判断
CollectionUtils.isNotEmpty(carriageRefs)
4.XML字符串转换对象或进行XML操作
4.1 利用 JAXBContext
JAXBContext jaxbContext = JAXBContext.newInstance(cls);
Unmarshaller um = jaxbContext.createUnmarshaller();
t = (T)um.unmarshal(new StringReader(xmlStr));
4.2 利用 XStream
if(! xmap.containsKey(cls.getName())){
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(cls);
xstream.ignoreUnknownElements();
xmap.put(cls.getName(), xstream);
}
T t = (T) xmap.get(cls.getName()).fromXML(xmlStr);
4.3利用 DocumentHelper.parseText(xml)
Node eventType = document.selectSingleNode("//logisticsEvent/eventHeader/eventType");
if (eventType != null && StringUtils.isNotBlank(eventType.getText())) {
type = CainiaoMessageEventTypeEnum.valueOf(eventType.getText().trim());
}
5. 枚举转换:
int packageType = PackageTypeEnum.SMALL.getIndex();
public enum PackageTypeEnum {
/**大包裹*/
BIG(1, "大包裹"),
/**小包裹*/
SMALL(2, "小包裹");
@Getter
@Setter
private int index;
@Getter
@Setter
private String name;
private PackageTypeEnum(int index,String name) {
this.index = index;
this.name = name;
}
public static String getName(int index) {
for (PackageTypeEnum s : PackageTypeEnum.values()) {
if (s.getIndex() == index) {
return s.name;
}
}
return null;
}
}
6.类型转换:
pkg.setWeight(BigDecimal.valueOf(jobPackageDTO.getWeight()));
7.字符串判断是否为空
StringUtils.isBlank(rCode)
8.数组转换成ArrayList
String[] mailNos=mailNo.replaceAll(" ", "").split(",");
List<String> mailnoList=Arrays.asList(mailNos);
9.判断对象类型:
other instanceof DataExample;
10.数组转换成List类型
String[] arrayA = new String[] { "1", "2", "3", "3", "4", "5" };
String[] arrayB = new String[] { "3", "4", "4", "5", "6", "7" };
List<String> a = Arrays.asList(arrayA);
List<String> b = Arrays.asList(arrayB);