In Java, data in JSON format is converted into Bean, map, and list data.

Source: Internet
Author: User

Simple Description:

To facilitate data transmission between the client and the server, sometimes we use some data types that are more convenient to organize, such as JSON and XML, to the client. The client can also re-organize the data and send it back to the server. JSON and XML provide a set of convenient data organization forms .. There are also many ready-made class libraries used to parse the data ..

This article summarizes the use of net. SF. JSON .. At present, there are many official open-source projects that process JSON, fastjson, and others .. You can choose to use different dependent packages depending on the complexity of your problem ..

If you use Maven to build a project, you need to depend on the following two packages:

<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.2.3</version></dependency><dependency><groupId>xom</groupId><artifactId>xom</artifactId><version>1.2.5</version></dependency>

If you use eclipse to build your own project, you need to manually download the dependency package for dependency.

Example program:

1. Convert the object data type assembled in JSON to the simple bean object corresponding to Java (only including the simple data type)

/*** Convert the data of the simple object type encapsulated in JSON format into a simple type JavaBean * @ return */private static object json2simplebean () {string jsonstr = "{\" Age \ ": 23, \" ID \ ": 123, \" Name \ ": \" tt_2009 \", "+" \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"} "; jsonobject jsonbean = jsonobject. fromobject (jsonstr); Return jsonobject. tobean (jsonbean, simpleuser. class );}

The above code converts a JSON data string to a simple bean customized in Java. Simpleuser: Check the appendix of the final code.

2. Convert JSON-encapsulated complex data into complex Java data entities (objects contain other complex objects, such as receiving addresses stored in List)

/*** Convert the complex Entity Data encapsulated in JSON format into a complex type of JavaBean * @ return */private static object json2complexbean () {string jsonstr = "{\" address \ ": [\" Beijing \ ", \" Shanghai \ ", \" Guangzhou \ "], "+" \ "Age \": 23, \ "ID \": 123, \ "Name \": \ "tt_2009 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"} "; jsonobject jsonbean = jsonobject. fromobject (jsonstr); Return jsonobject. tobean (jsonbean, complexuser. class );}

The code above converts a JSON string to a custom complex data entity in Java. Complexuser: Check the code in the last appendix.

3. Convert JSON data to list in Java

/*** Convert the list data encapsulated in JSON format to Java List Data * @ return */private static object json2list () {string jsonarray = "[{\" Age \ ": 23, \" ID \ ": 123, \" Name \ ": \" tt_2009_0 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"}, "+" {\ "Age \": 24, \ "ID \": 123, \ "Name \": \ "tt_2009_1 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"}, "+" {\ "Age \": 32, \ "ID \": 123, \ "Name \": \ "tt_2009_9 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"}] "; return jsonarray. tolist (jsonarray. fromobject (jsonarray), new simpleuser (), new jsonconfig ());}

The code above converts the data assembled in JSON to the List data in Java. For more complex data processing, follow-up articles, such as processing date and other format data.

4. Convert JSON data to map in Java. (Note that the key can only be a string)

/*** Converts string data encapsulated in JSON format to map data in Java * @ return */Private Static Map <string, simpleuser> json2map () {Map <string, simpleuser> map = new hashmap <string, simpleuser> (); string jsonmapstr = "{\" tt_2009_4 \ ": {\" Age \ ": 27, \" ID \": 123, \ "Name \": \ "tt_2009_4 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"}, "+" \ "tt_2009_6 \" :{\ "Age \": 29, \ "ID \": 123, \ "Name \": \ "tt_2009_6 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"}, "+" \ "tt_2009_0 \": {\ "Age \": 23, \ "ID \": 123, \ "Name \": \ "tt_2009_0 \", \ "province \": \ "Shanghai \", \ "Sex \": \ "male \"} "; jsonobject jsonmap = jsonobject. fromobject (jsonmapstr); iterator <string> it = jsonmap. keys (); While (it. hasnext () {string key = (string) it. next (); simpleuser u = (simpleuser) jsonobject. tobean (jsonobject. fromobject (jsonmap. get (key), simpleuser. class); map. put (Key, U);} return map ;}

The above implementation is to convert the JSON-assembled key-value format data into a map in Java.

Appendix code:

(Methods such as setter and getter are omitted)

/*** Simple entity class that only contains the basic data type * @ author tt_2009 */public class simpleuser {private int ID; private string name; private string sex; private int age; private string province ;}

/*** Contains complex object classes * @ author tt_2009 */public class complexuser extends simpleuser {private list <string> address; Public complexuser () {} public complexuser (int id, string name, string sex, int age, string province, list <string> address) {super (ID, name, sex, age, province ); this. address = address;} public list <string> getaddress () {return address;} public void setaddress (list <string> address) {This. address = address ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.