In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve interconversion between JSON and Javabean". In daily operation, I believe many people have doubts about how to achieve interconversion between JSON and Javabean. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve interconversion between JSON and Javabean". Next, please follow the editor to study!
The difference between JSONObject and JSONArray
JSONObject:
{"area": "Wuhan", "name": "Zhang San", "age": 25}
JSONArray:
[{"area": "Wuhan", "name": "Zhang San", "age": 25}, {"area": "Shenzhen", "name": "Li Si", "age": 22}]
Generally speaking, JSONObject is the json form of an object JSONArry is the JSON form of a collection of objects.
JSON and javabean transfer each other
JSON uses Ali's fastjson bag.
Use case java object
Public class User {protected Long id; protected String account; protected String password; protected String name; protected boolean gender; protected String telephone; @ Override public String toString () {return "User {" + "id=" + id + ", account='" + account +''+ ", password='" + password +''+ ", name='" + name +''+ ", gender=" + gender + ", telephone='" + telephone +''+'}' } public boolean isGender () {return gender;} public void setGender (boolean gender) {this.gender = gender;} public String getTelephone () {return telephone;} public void setTelephone (String telephone) {this.telephone = telephone;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Long getId () {return id;} public void setId (Long id) {this.id = id } public String getAccount () {return account;} public void setAccount (String account) {this.account = account;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}} 1, javabean to json
Method 1: convert it to String and then to JSONObject through java object
Package com.handoop.gms.utils;import com.alibaba.fastjson.JSONObject;import com.handoop.gms.domain.User;public class TestMain {public static void main (String [] args) {/ / first initialize an object User user=new User ((long) 1, "admin", "admin", "Zhang San", true, "123456") through the constructor; / / convert the java object to String type String jsonString= JSONObject.toJSONString (user) / / change the String type to JSONObject JSONObject jsonObject=JSONObject.parseObject (jsonString); System.out.println (jsonObject); / / after converting to JSONObject, you can get his element System.out.println (jsonObject.get ("password")) at any time according to the key value;}}
Method 2:java object is transferred directly to json
Package com.handoop.gms.utils;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.handoop.gms.domain.User;public class TestMain {public static void main (String [] args) {/ / initialize an object User user=new User ((long) 1, "admin", "admin", "Zhang San", true, "123456"); JSONObject jsonObject= (JSONObject) JSONObject.toJSON (user); System.out.println (jsonObject) }} json string to JSONObejectpublic class TestMain {public static void main (String [] args) {String str= "{" password ":" admin "," gender ": true," name ":" Zhang San "," telephone ":" 123456 "," id ": 1," account ":" admin "}" JSONObject jsonObject=JSONObject.parseObject (str); System.out.println ("account:" + jsonObject.get ("account") + "-" + "paasword:" + jsonObject.get ("password")) }} 3.jsonString to JSONArraypublic class TestMain {public static void main (String [] args) {String str= "{" data ": [{" password ":" admin "," gender ": true," name ":" Zhang San "," telephone ":" 123456 "," id ": 1," account ":" admin "}]}"; / first to JSONObject JSONObject jsonObject=JSONObject.parseObject (str); / / to JSONArray JSONArray jsonArray=jsonObject.getJSONArray ("data") from the array type data in JSONObject System.out.println (jsonArray.get (0));}} 4.JSON string to JAVA object String str= "{" password ":" admin "," gender ": true," name ":" Zhang San "," telephone ":" 123456 "," id ": 1," account ":" admin "}"; / / JSON string followed by java object type User user=JSONObject.parseObject (str,User.class) System.out.println ("account:" + user.getAccount () + "-" + "paasword:" + user.getPassword ()); at this point, the study on "how JSON and Javabean convert each other" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.