jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
json与对象互转
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ApplymentsRequest {
/**
*out_request_no json的字段名称
*/
@JsonProperty(value = "out_request_no")
private String outRequestNo;
@JsonProperty(value = "organization_type")
private String organizationType;
}
工具方法
/**
* <pre>
* 将对象转换成json字符串
* </pre>
*
* @param object 实例对象
* @return String
*/
public static String bean2Json(Object object) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
return objectMapper.writeValueAsString(object);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* <pre>
* 将json字符串转化成对象
* </pre>
*
* @param clazz 转化成的对象类型
* @param json 需要转化的json字符串
* @param <T> 泛型
* @return T
*/
public static <T> T json2Bean(Class<T> clazz, String json) {
if (json == null) {
return null;
}
if (json.contains("<html>")) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
try {
return objectMapper.readValue(json, clazz);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* <pre>
* 将json字符串转化成List对象
* </pre>
*
* @param clazz Class类型
* @param json json字符串
* @param <T> 泛型
* @return List
*/
public static <T> List<T> json2List(Class<T> clazz, String json) {
if (json == null || json.contains("<html>")) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
JavaType javaType = getCollectionType(objectMapper, ArrayList.class, clazz);
return objectMapper.readValue(json, javaType);
} catch (Exception ignored) {
}
return null;
}
/**
* @param clazz Class类型
* @param json json字符串
* @param <T> 泛型
* @return T
*/
public static <T> Map<String, T> json2Map(Class<T> clazz, String json) {
if (json == null || json.contains("<html>")) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
JavaType javaType = getCollectionType(objectMapper, clazz);
return objectMapper.readValue(json, javaType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static JavaType getCollectionType(ObjectMapper objectMapper, Class<?> collectionClass, Class<?>... elementClasses) {
TypeFactory typeFactory = objectMapper.getTypeFactory();
return typeFactory.constructParametricType(collectionClass, elementClasses);
}
public static JavaType getCollectionType(ObjectMapper objectMapper, Class<?> elementClasse) {
TypeFactory typeFactory = objectMapper.getTypeFactory();
return typeFactory.constructMapType(TreeMap.class, String.class, elementClasse);
}
xml与对象互转
@XStreamAlias("xml")
@Data
public class WxPayUnifiedOrderRequest{
@XStreamAlias("version")
private String version;
}
工具方法
public String toXML() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(this.getClass());
return xstream.toXML(this);
}
public static WxPayOrderNotifyResult fromXML(String xmlString) {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxPayOrderNotifyResult.class);
xstream.registerConverter(new WxPayOrderNotifyResultConverter(xstream.getMapper(), xstream.getReflectionProvider()));
WxPayOrderNotifyResult result = (WxPayOrderNotifyResult) xstream.fromXML(xmlString);
result.setXmlString(xmlString);
return result;
}