Conversion between Java objects and JSON objects

Source: Internet
Author: User
Reference http://www.cnblogs.com/linjiqin/archive/2011/09/19/2181408.html

In the process of mobile Internet development, data service transmission is generally dominated by xml json. JSON is more convenient for object usage, so let's look at it.

Public class siminfo {private string IMEI; private string imsi; private string telephone; Public String getimei () {return IMEI;} public void setimei (string IMEI) {This. IMEI = IMEI;} Public String getimsi () {return imsi;} public void setimsi (string imsi) {This. imsi = imsi;} Public String gettelephone () {return telephone;} public void settelephone (string telephone) {This. telephone = telephone ;}}

The following JAR packages are required

1. convert a Java object to a JSON object and reverse parse and restore it

Siminfo = new siminfo ();
Siminfo. settelephone ("1111 ");
Siminfo. setimei ("2222 ");
Siminfo. setimsi ("3333 ");

String JSON = new jsonarray (). fromobject (siminfo). tostring ();
System. Out. println ("JSON:" + JSON );

JSON = JSON. substring (1, JSON. Length ()-1 );
Jsonobject OBJ = new jsonobject (). fromobject (JSON); // convert from a JSON object to a Java object
Siminfo siminfo_t = (siminfo) jsonobject. tobean (OBJ, siminfo. Class );
System. Out. println ("OBJ:" + siminfo_t );
System. Out. println (siminfo_t.getimei ());
System. Out. println (siminfo_t.getimsi ());
System. Out. println (siminfo_t.gettelephone ());

--------------- The execution result is as follows:

JSON: [{"IMEI": "2222", "imsi": "3333", "telephone": "1111"}]
OBJ: siminfo @ 1ac04e8
2222
3333
1111

2. convert a Java list object to a JSON object and reverse parse and restore it

Siminfo = new siminfo ();
Siminfo. settelephone ("1111 ");
Siminfo. setimei ("2222 ");
Siminfo. setimsi ("3333 ");
// Convert a Java object to a JSON object

Siminfo siminfo2 = new siminfo ();
Siminfo2.settelephone ("1111x ");
Siminfo2.setimei ("2222x ");
Siminfo2.setimsi ("3333x ");

List <siminfo> List = new arraylist <siminfo> ();
List. Add (siminfo );
List. Add (siminfo2 );

String JSON = new jsonarray (). fromobject (list). tostring ();
System. Out. println ("JSON:" + JSON );

List <siminfo> siminfos =Getjavacollection(New siminfo (), JSON );
System. Out. println (siminfos. Size ());
For (siminfo siminfo1: siminfos ){
System. Out. println ("IMEI:" + siminfo1.getimei ());
System. Out. println ("========= ");
}

Private Static <t> List <t>Getjavacollection(T clazz, string jsons ){
List <t> objs = NULL;
Jsonarray = (jsonarray) jsonserializer. tojson (jsons );
If (jsonarray! = NULL ){
Objs = new arraylist <t> ();
List list = (list) jsonserializer. tojava (jsonarray );
For (Object O: List ){
Jsonobject = jsonobject. fromobject (O );
T OBJ = (t) jsonobject. tobean (jsonobject, clazz. getclass ());
Objs. Add (OBJ );
}
}
Return objs;
}

--------------- The execution result is as follows:

JSON: [{"IMEI": "2222", "imsi": "3333", "telephone": "1111" },{ "IMEI": "2222x ", "imsi": "3333x", "telephone": "1111x"}]
2
IMEI: 2222.
==========
IMEI: 2222x
==========

3. supplement the instance. If the class object contains a list object, whether the conversion can be normal or not, the answer is yes. See the following example.

Public class siminfo {

Private string IMEI;
Private string imsi;
Private string telephone;
Private list <replay> re;
Public String getimei (){
Return IMEI;
}
Public void setimei (string IMEI ){
This. IMEI = IMEI;
}
Public String getimsi (){
Return imsi;
}
Public void setimsi (string imsi ){
This. imsi = imsi;
}
Public String gettelephone (){
Return telephone;
}
Public void settelephone (string telephone ){
This. Telephone = telephone;
}
Public list <replay> getre (){
Return re;
}
Public void setre (list <replay> re ){
This. Re = Re;
}

}

Siminfo = new siminfo ();
Siminfo. settelephone ("1111 ");
Siminfo. setimei ("2222 ");
Siminfo. setimsi ("3333 ");
List <replay> Re = new arraylist <replay> ();
Re. Add (New replay ("title1", "name1 "));
Re. Add (New replay ("title2", "name2 "));
Siminfo. setre (re );
// Convert a Java object to a JSON object

String JSON = new jsonarray (). fromobject (siminfo). tostring ();
System. Out. println ("JSON:" + JSON );

JSON = JSON. substring (1, JSON. Length ()-1 );
Jsonobject OBJ = new jsonobject (). fromobject (JSON );
Siminfo siminfo_t = (siminfo) jsonobject. tobean (OBJ, siminfo. Class );
System. Out. println ("OBJ:" + siminfo_t );
System. Out. println (siminfo_t.getimei ());
System. Out. println (siminfo_t.getimsi ());
System. Out. println (siminfo_t.gettelephone ());
System. Out. println (siminfo_t.getre (). Size ());

--------------- The execution result is as follows:

JSON: [{"IMEI": "2222", "imsi": "3333", "re": [{"name": "name1", "title ": "title1" },{ "name": "name2", "title": "title2"}], "telephone": "1111"}]
OBJ: siminfo @ df6ccd
2222
3333
1111
2

Related Article

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.