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

How to convert and use List and Map in FastJson

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to change the use of List and Map in FastJson, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

The conversion method of List and Map of basic type List

Convert List to Json string

List strList = new ArrayList (); strList.add ("str1"); strList.add ("str2"); String inputStr = JSON.toJSONString (strList); System.out.println (inputStr)

Json string is converted to List

List outputList = JSON.parseObject (inputStr, List.class); outputList.forEach (System.out::println); Map

Convert Map to Json string

Map strMap = new HashMap (); strMap.put ("key1", "value1"); strMap.put ("key2", "value2"); String inputMap = JSON.toJSONString (strMap); System.out.println (inputMap)

Json string is converted to Map

Map stringMap = JSON.parseObject (inputMap, Map.class); stringMap.forEach ((key, value)-> System.out.println (key + ":" + value)); List and Map conversion mode List of Bean type

Convert List to Json string

List inputList = new ArrayList (); inputList.add (new Bean (1, "po1")); inputList.add (new Bean (2, "po2")); String inputStr = JSON.toJSONString (inputList); System.out.println (inputStr)

Json string is converted to List

List outputList = JSON.parseArray (inputStr, Bean.class); outputList.forEach (po-> System.out.println (po.getNum () + "name:" + po.getName ()); Map

Convert Map to Json string

Map strMap = new HashMap (); strMap.put ("key1", new Bean (1, "po1"); strMap.put ("key2", new Bean (2, "po2")); String inputMap = JSON.toJSONString (strMap); System.out.println (inputMap)

Json string is converted to Map

Map stringMap = JSON.parseObject (inputMap, Map.class); stringMap.forEach ((key, value)-> {Bean mock = JSON.parseObject (value.toJSONString (), Bean.class); System.out.println (key + ":" + mock.getName ();}); List and Map of Bean type (TypeReference) use TypeReference to convert Json strings into List Type listType = new TypeReference () {} .getType () List outputList = JSON.parseObject (inputStr, listType); outputList.forEach (po-> System.out.println (po.getNum () + "name:" + po.getName ()); use TypeReference to convert the Json string into Map Type mapType = new TypeReference () {} .getType (); Map stringMap = JSON.parseObject (inputMap, mapType); stringMap.forEach ((key, value)-> {System.out.println (key + ":" + value.getName () The above is all the contents of the article "how to change the use of List and Map in FastJson". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Development

Wechat

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

12
Report