In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to convert between javaBean and json objects, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Conversion between java Bean and json objects
/ * * one JSON.parseObject () single object or map curly braces {} * two JSON.parseArray () multiple object arrays [] complex to define classes in the class * conversion between three JSON.toJSONString () javaBean and JSON format strings * * conversion between four javaBean and json objects is used: JSON.toJSON (obj), and then use forced type conversion JSONObject or JSONArray * / @ Test public void testJson () {/ / {"name": "Xiao Hong", "age": 21, "nation": "China"} format to format "\" String jsonPerson= "{\" name\ ":\" Xiao Hong\ ",\" age\ ": 21,\" nation\ ":\" China\ " System.out.println (jsonPerson); / / {"name": "Xiao Hong", "age": 21, "nation": "China"} String s = JSON.toJSONString (jsonPerson); / / "{\" name\ ":\" Xiao Hong\ ",\" age\ ": 21,\" nation\ ":\" China\ "}" System.out.println (s) / [{"text": "memory size", "value": "10m"}, {"text": "Color", "value": "Red"}] / / json is converted to object Person person = JSON.parseObject (jsonPerson,Person.class); System.out.println (person); System.out.println ("=") String jsonPerson2= "[{\" name\ ":\" Xiao Hong\ ",\" age\ ": 21,\" nation\ ":\" China\ "}, {\" name\ ":\" Xiao Hong 2\ ",\" age\ ": 31,\" nation\ ":\" China 2\ "}"; / / Person person1 = JSON.parseObject (jsonPerson2, Person.class); / / System.out.println (person1) / com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0 List personList = JSON.parseArray (jsonPerson2, Person.class); System.out.println (personList); System.out.println ("="); String result = "{\ n" + "\" success\ ":\" true\ ",\ n" + "\ n" + "\" returnAddress\ ":\" 123\ "\ n" + "\ n}"; JSONObject jsonObject = JSON.parseObject (result); System.out.println (jsonObject) / / {"success": "true", "returnAddress": "123"} Map map = JSON.parseObject (result, Map.class); System.out.println (map); / / {success=true, returnAddress=123} System.out.println (" value obtained by map "+ map.get (" success ")); / / trueSystem.out.println (jsonObject.toJSONString ()) when value obtained by map; / / {" success ":" true "," returnAddress ":" 123 "} Map jsonObject1 = (Map) JSON.parseObject (result) System.out.println (jsonObject1); / / {"success": "true", "returnAddress": "123"} System.out.println ("value obtained by jsonObject1" + jsonObject1.get ("success")); / / the method of true} / / multiple javabean is to traverse the value obtained by jsonObject1
The difference between JSON.parseObject (String str) and JSONObject.parseObject (String str)
According to the source code: JSON is an abstract class, there is a static method parseObject (String text) in JSON, which parses text into a JSONObject object and returns; JSONObject is a class inherited from JSON, when JSONObject.parseObject (result) is called, the parent class's parseObject (String text) is called directly. So there is no difference between the two, one is to call the parent class's own static parseObject (String text) with the parent class, and the other is to call the parent class's static parseObject (String text) with the subclass, both of which call the same method.
The above content is how to convert between javaBean and json objects. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.