Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert and use List and Map in FastJson

Shulou Source: shulou.com Published: 2022-06-03 18:41:20 11月12日 Update

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!

Tags: Characters strings methods articles types content not much basic most more knowledge industry information information channels channels high-level reference learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Redmi Huawei Microsoft