[英]Serializing an array of arrays in java using Jackson
我正在學習如何使用 Jackson,我必須序列化一組 ContoCorrente 對象
public class ContoCorrente {
private String proprietario;
private ArrayList<MovimentoBancario> movimenti;
//methods...
}
其中 MovimentoBancario 被定義為
public class MovimentoBancario {
private String data;
private String causale;
//methods...
}
我試過
ArrayList<ContoCorrente> conti= new ArrayList<ContoCorrente>(50);
for(int i=0; i<50; i++){
//fill array
}
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
System.out.println(mapper.writeValueAsString(conti));
} catch (Exception e) {
e.printStackTrace();
}
但輸出是
[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
我不知道我做錯了什么,這是我第一次嘗試序列化
您可以嘗試使用 Jackson 定義特定方法。
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectReader reader = new ObjectMapper().readerFor(Array.class);
public static String toArray(ArrayList<ContoCorrente> array) throws JsonProcessingException {
return objectMapper.writeValueAsString(array);
}
然后,如果您想閱讀,請使用閱讀器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.