Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What kind of pit is encountered in JSONArray conversion?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the pit encountered in JSONArray conversion, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.

Let's first show a piece of code:

List initList = initEnableRecoveryChoose (null); if (CollectionUtils.isEmpty (initList)) return null;String elistStr = JSONObject.toJSONString (initList); net.sf.json.JSONArray jsonArray = JSONArray.fromObject (elistStr); List enableChList = (List) jsonArray.toCollection (jsonArray, DataRecoveryEnableDto.class)

In the above code, the data from the initial initList query is shown in the figure:

However, after converting to a json string and then to List, it is found that the time in the object becomes the current time:

What about the object conversion we talked about? There is always a gap between the result and imagination.

Let's first look at what is converted to a json string:

View the contents of the string:

[{"fromNodeCode": "2", "fromNodeName": "new cases", "ruleOrdinal": "18", "toNodeCode": "2", "toNodeName": "new cases", "transDate": 1562601600000}, {"fromNodeCode": "2", "fromNodeName": "new cases", "ruleOrdinal": "24", "toNodeCode": "3", "toNodeName": "collection system", "transDate": 1562601600000}, {"fromNodeCode": "2", "fromNodeName": "new cases" "ruleOrdinal": "22", "toNodeCode": "4", "toNodeName": "Direct reminder system", "transDate": 1562601600000}, {"fromNodeCode": "2", "fromNodeName": "New case", "ruleOrdinal": "23", "toNodeCode": "4", "toNodeName": "Direct Enforcement system", "transDate": 1562601600000}, {"fromNodeCode": "3", "fromNodeName": "Collection system", "ruleOrdinal": "21", "toNodeCode": "4" "toNodeName": "Direct reminder system", "transDate": 1562601600000}, {"fromNodeCode": "3", "fromNodeName": "Collection system", "ruleOrdinal": "12", "toNodeCode": "5", "toNodeName": "customer Management system", "transDate": 1562601600000}, {"fromNodeCode": "4", "fromNodeName": "Direct Enforcement system", "ruleOrdinal": "24", "toNodeCode": "3", "toNodeName": "Collection system", "transDate": 1562601600000} {"fromNodeCode": "4", "fromNodeName": "Direct prompt system", "ruleOrdinal": "25", "toNodeCode": "5", "toNodeName": "customer system", "transDate": 1562601600000}, {"fromNodeCode": "5", "fromNodeName": "customer system", "ruleOrdinal": "3", "toNodeCode": "2", "toNodeName": "New case", "transDate": 1562601600000}, {"fromNodeCode": "2", "fromNodeName": "New case" "ruleOrdinal": "24", "toNodeCode": "3", "toNodeName": "Express system", "transDate": 1562688000000}]

After testing, we can find that when converting List to json string, the time is converted to long type, but after conversion, we can find that it is still 9 days time:

View the time of each object in jsonArray:

As shown in the figure, you can find that it is the time corresponding to the string, so there should be no problem here. It can be concluded that the problem should occur in this sentence:

List enableChList = (List) jsonArray.toCollection (jsonArray, DataRecoveryEnableDto.class)

By following up the source code, we can find that the long type of the processing that should be carried out in this method is date:

If we continue to follow up, we can find that the newInstance operation is carried out according to the type of java.util.Date in the source code, which is equivalent to the operation of new Date.

From the above, we can find that when converting a json string of type String to List, try not to use the following methods, unless there is no requirement for a date:

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

Add test code to test:

Public static void main (String [] args) {long T1 = 1562601600000l; long T2 = 15626880000l; List initList = new ArrayList (); for (int ionometer I {System.out.println (dto.getFromNodeCode () + "+ dto.getToNodeCode () +"-"+ dto.getTransDate ());}); System.out.println (JSONObject.toJSONString (list));}

Execution result:

As shown in the picture, you can find that this way of using Alibaba is still easy to use:

String elistStr = JSONObject.toJSONString (initList)

JSONArray array = JSONArray.parseArray (elistStr)

List list = array.toJavaList (DataRecoveryEnableDto.class)

At this point, I hope I can help passing friends!

The above is about the pit encountered in JSONArray conversion, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report