您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)關(guān)于JSONArray轉(zhuǎn)換遇到的坑是怎么樣的,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
先展示一段代碼:
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查詢出來的數(shù)據(jù)如圖:
但是經(jīng)過轉(zhuǎn)換為json字符串,然后再轉(zhuǎn)換為L(zhǎng)ist后發(fā)現(xiàn)對(duì)象里面的時(shí)間卻變成了當(dāng)前時(shí)間:
說好的對(duì)象轉(zhuǎn)換呢?結(jié)果與想象總是有所差距
我們先查看轉(zhuǎn)換為json字符串后的內(nèi)容:
查看串內(nèi)容:
[{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"18","toNodeCode":"2","toNodeName":"新增案件","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"催收系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"22","toNodeCode":"4","toNodeName":"直催系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"23","toNodeCode":"4","toNodeName":"直催系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"3","fromNodeName":"催收系統(tǒng)","ruleOrdinal":"21","toNodeCode":"4","toNodeName":"直催系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"3","fromNodeName":"催收系統(tǒng)","ruleOrdinal":"12","toNodeCode":"5","toNodeName":"客戶管理系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"4","fromNodeName":"直催系統(tǒng)","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"催收系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"4","fromNodeName":"直催系統(tǒng)","ruleOrdinal":"25","toNodeCode":"5","toNodeName":"客戶系統(tǒng)","transDate":1562601600000},{"fromNodeCode":"5","fromNodeName":"客戶系統(tǒng)","ruleOrdinal":"3","toNodeCode":"2","toNodeName":"新增案件","transDate":1562601600000},{"fromNodeCode":"2","fromNodeName":"新增案件","ruleOrdinal":"24","toNodeCode":"3","toNodeName":"快信系統(tǒng)","transDate":1562688000000}]
經(jīng)過測(cè)試可以發(fā)現(xiàn),在將List轉(zhuǎn)換為json字符串的時(shí)候,時(shí)間被轉(zhuǎn)換為了long類型,但經(jīng)過轉(zhuǎn)換可以發(fā)現(xiàn)還是9日的時(shí)間:
查看jsonArray中的每個(gè)對(duì)象的時(shí)間:
如圖可以發(fā)現(xiàn)就是字符串對(duì)應(yīng)的時(shí)間,所以到此處應(yīng)該還是沒有問題的,可以斷定問題應(yīng)該是出現(xiàn)在這一句:
List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>) jsonArray.toCollection(jsonArray, DataRecoveryEnableDto.class);
通過跟進(jìn)源碼可以發(fā)現(xiàn)應(yīng)該是在這個(gè)方法里面進(jìn)行的處理long類型為日期的:
繼續(xù)跟進(jìn)可以發(fā)現(xiàn),這個(gè)源碼里面是按照java.util.Date的類型進(jìn)行了newInstance操作,也就是等價(jià)于new Date的操作
綜合上面可以發(fā)現(xiàn),將String類型的json串轉(zhuǎn)換為L(zhǎng)ist的時(shí)候,盡量不要使用如下方式,除非對(duì)日期沒有要求:
net.sf.json.JSONArray jsonArray = JSONArray.fromObject(elistStr); List<DataRecoveryEnableDto> enableChList = (List<DataRecoveryEnableDto>) jsonArray.toCollection(jsonArray, DataRecoveryEnableDto.class);
添加測(cè)試代碼進(jìn)行測(cè)試:
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)); }
執(zhí)行結(jié)果:
如圖可以發(fā)現(xiàn)使用阿里巴巴的這種方式還是好使的:
String elistStr = JSONObject.toJSONString(initList);
JSONArray array = JSONArray.parseArray(elistStr);
List<DataRecoveryEnableDto> list = array.toJavaList(DataRecoveryEnableDto.class);
至此,希望可以幫助路過的友人!
以上就是關(guān)于JSONArray轉(zhuǎn)換遇到的坑是怎么樣的,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。