把json字符串轉(zhuǎn)化為嵌套對(duì)象
/**
*多個(gè)對(duì)象之間依賴關(guān)系糕簿,并且多個(gè)類都寫在一個(gè)類里面,內(nèi)部類必須使用static修飾
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class FBLoginInfo {
@JsonProperty(value = "active_sessions")
private List<LoginInfo> activeSessions;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
static class LoginInfo{
private String location;
private String app;
private String device;
@JsonProperty(value = "ip_address")
private String ip;
@JsonProperty(value = "created_timestamp")
private Long createdTimestamp;
}
}
把json字符串轉(zhuǎn)化為對(duì)象數(shù)組
public class FileParseUtil {
private final static ObjectMapper mapper = new ObjectMapper();
public static List parse(String jsonStr, Class T){
try{
JavaType javaType = getCollectionType(ArrayList.class,T);
List configList = mapper.readValue(jsonStr, javaType);
return configList;
}catch (Exception e){
log.error("解析文件錯(cuò)誤");
}
return null;
}
private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者