關於JSONArray轉換遇到的坑

先展現一段代碼:java

List<DataRecoveryEnableDto> initList = initEnableRecoveryChoose(null);
if(CollectionUtils.isEmpty(initList)) return null;

String elistStr = JSONObject.toJSONString(initList);

net.sf.json.JSONArray jsonArray = JSONArray.fromObject(elistStr);
List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>) jsonArray.toCollection(jsonArray, DataRecoveryEnableDto.class);

上面的代碼中,一開始的initList查詢出來的數據如圖:json

可是通過轉換爲json字符串,而後再轉換爲List後發現對象裏面的時間卻變成了當前時間:測試

 

說好的對象轉換呢?結果與想象老是有所差距對象

咱們先查看轉換爲json字符串後的內容:blog

查看串內容:字符串

[{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"18","toNodeCode":"2","toNodeName":"新增案件","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"催收系統","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"22","toNodeCode":"4","toNodeName":"直催系統","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"23","toNodeCode":"4","toNodeName":"直催系統","transDate":1562601600000},{"fromNodeCode":"3","fromNodeName":"催收系統","ruleOrdinal":"21","toNodeCode":"4","toNodeName":"直催系統","transDate":1562601600000},{"fromNodeCode":"3","fromNodeName":"催收系統","ruleOrdinal":"12","toNodeCode":"5","toNodeName":"客戶管理系統","transDate":1562601600000},{"fromNodeCode":"4","fromNodeName":"直催系統","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"催收系統","transDate":1562601600000},{"fromNodeCode":"4","fromNodeName":"直催系統","ruleOrdinal":"25","toNodeCode":"5","toNodeName":"客戶系統","transDate":1562601600000},{"fromNodeCode":"5","fromNodeName":"客戶系統","ruleOrdinal":"3","toNodeCode":"2","toNodeName":"新增案件","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"快信系統","transDate":1562688000000}]get

通過測試能夠發現,在將List轉換爲json字符串的時候,時間被轉換爲了long類型,但通過轉換能夠發現仍是9日的時間:源碼

 

查看jsonArray中的每一個對象的時間:it

如圖能夠發現就是字符串對應的時間,因此到此處應該仍是沒有問題的,能夠判定問題應該是出如今這一句:io

List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>) jsonArray.toCollection(jsonArray, DataRecoveryEnableDto.class);

經過跟進源碼能夠發現應該是在這個方法裏面進行的處理long類型爲日期的:

繼續跟進能夠發現,這個源碼裏面是按照java.util.Date的類型進行了newInstance操做,也就是等價於new Date的操做

 

綜合上面能夠發現,將String類型的json串轉換爲List的時候,儘可能不要使用以下方式,除非對日期沒有要求:

net.sf.json.JSONArray jsonArray = JSONArray.fromObject(elistStr);
List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>) jsonArray.toCollection(jsonArray, DataRecoveryEnableDto.class);

 

添加測試代碼進行測試:

public static void main(String[] args) {
    long t1 = 1562601600000l;
    long t2 = 1562688000000l;
    List<DataRecoveryEnableDto> initList = new ArrayList<>();
    for(int i=0;i<8;i++){
        DataRecoveryEnableDto enableDto = new DataRecoveryEnableDto();
        enableDto.setFromNodeCode(i+"");
        enableDto.setToNodeCode((i+2)+"");
        enableDto.setTransDate(i%2==0?new Date(t1):new Date(t2));
        initList.add(enableDto);
    }

    String elistStr = JSONObject.toJSONString(initList);

    JSONArray array = JSONArray.parseArray(elistStr);
    List<DataRecoveryEnableDto> list = array.toJavaList(DataRecoveryEnableDto.class);
    List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>)JSONObject.parseObject(elistStr,initList.getClass());
    list.stream().forEach(dto->{
        System.out.println(dto.getFromNodeCode()+"-"+dto.getToNodeCode()+"-"+dto.getTransDate());
    });
    System.out.println(JSONObject.toJSONString(list));
}

執行結果:

如圖能夠發現使用阿里巴巴的這種方式仍是好使的:

String elistStr = JSONObject.toJSONString(initList);

JSONArray array = JSONArray.parseArray(elistStr);

List<DataRecoveryEnableDto> list = array.toJavaList(DataRecoveryEnableDto.class);

至此,但願能夠幫助路過的友人!

相關文章
相關標籤/搜索