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 use the Java tool library FastJson

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Java toollibrary FastJson how to use", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java Toollibrary FastJson how to use" article.

FastJson converts json format strings into List collections

Note: the json format string must conform to the array format such as [{"a": a}, {"b": B}]

Scenario 1: the front end passes json strings in array format to the background, and how to convert them into List collections

List readJson2List = JSON.parseArray (json, AccountBean.class) Note this is Bean.class, not List.class

@ Test public void readJson2ListBean () {String json = "[{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}, {" address ":" address "," name ":" "," id ": 1," email ":" email "]"; List readJson2List = JSON.parseArray (json, AccountBean.class) / / output test JsonUtils.beanToJson (readJson2List);}

Scenario 2: the front end passes json strings in array format to the background, and how to convert them into List collections

@ Test public void readJson2ListMap () {String json = "[{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}, {" address ":" address "," name ":" "," id ": 1," email ":" email "]"; List readJson2List = JSON.parseArray (json,HashMap.class) / / output test JsonUtils.beanToJson (readJson2List);} II. FastJson converts json format strings into objects

Scenario 1: the front end passes json characters in array format to the background and converts them into Bean objects.

@ Test public void readJson2Bean () {String json = "{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}"; AccountBean accountBean = JSON.parseObject (json,AccountBean.class); / / output test JsonUtils.beanToJson (accountBean);}

Scenario 2: the front end passes the Map collection of json strings in array format to the background

@ Test public void readJson2Map () {String json = "{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}"; HashMap accountBean = JSON.parseObject (json,Map.class); / / output test JsonUtils.beanToJson (accountBean) } 3. FastJson converts objects or collections into the json format string @ Test public void beanAndCollectionTOJson () {AccountBean bean = new AccountBean (); bean.setAddress ("china-Guangzhou"); bean.setEmail ("hoojo_@126.com"); bean.setId (1); bean.setName ("hoojo") String json = JSON.toJSONString (bean); System.out.println (json);} IV. FastJson converts json format strings into json objects @ Test public void jsonStringTOJsonBean () {String json = "{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}" JSONObject parse = (JSONObject) JSON.parse (json); String address= (String) parse.get ("address"); System.out.println ("output address==" + address) 5. FastJson converts json format strings into json array @ Test public void jsonString2JsonArray () {String json = "[{" address ":" address2 "," name ":" 2 "," id ": 2," email ":" email2 "}, {" address ":" address "," name ":" "," id ": 1," email ":" email "}]" JSONArray parseArray = JSON.parseArray (json); / / output test JsonUtils.beanToJson (parseArray.get (0));} VI. FastJson converts java objects into json objects @ Test public void beanAndCollectionTOJsonBean () {AccountBean bean = new AccountBean (); bean.setAddress ("china-Guangzhou") Bean.setEmail ("hoojo_@126.com"); bean.setId (1); bean.setName ("hoojo"); JSONObject json = (JSONObject) JSON.toJSON (bean); json.get ("address"); System.out.println (json) } the above is about the content of this article on "how to use the Java toollibrary FastJson". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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

Development

Wechat

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

12
Report