Converting between Java objects and JSON objects

Source: Internet
Author: User

A good example of the mutual transfer of Java objects and JSON objects:

1  Packagecom.kenneth.test;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 ImportJava.util.Iterator;6 Importjava.util.List;7 8 ImportNet.sf.json.JSONArray;9 ImportNet.sf.json.JSONObject;Ten  One /** A * Created by Administrator on 2016/2/1 0001. -  */ -  Public classJsonandjava { the  -      Public Static voidMain (string[] args) { - Testjsonbean (); - Testjsonattribute (); + Testjsonarray (); -     } +  A@SuppressWarnings ("Rawtypes") at     Private Static voidTestjsonarray () { -Student Student1 =NewStudent (); -Student1.setid (1); -Student1.setname ("Jag"); -Student1.setsex ("Man"); -Student1.setage (25); inStudent1.sethobby (Newstring[]{"basketball", "game"}); -  toStudent Student2 =NewStudent (); +Student2.setid (2); -Student2.setname ("Tom"); theStudent2.setsex ("Woman"); *Student2.setage (23); $Student2.sethobby (Newstring[]{"Surfing", "Running"});Panax Notoginseng  -list<student> list =NewArraylist<student>(); the List.add (student1); + List.add (student2); A  theJsonarray Jsonarray =jsonarray.fromobject (list); + System.out.println (jsonarray.tostring ()); -  $Jsonarray new_jsonarray=Jsonarray.fromobject (Jsonarray.toarray ()); $ //Collection java_collection=jsonarray.tocollection (new_jsonarray); - //if (java_collection!=null &&!java_collection.isempty ()) - //        { the //Iterator it=java_collection.iterator (); - //While (It.hasnext ())Wuyi //            { the //jsonobject Jsonobj=jsonobject.fromobject (It.next ()); - //Student stu= (Student) Jsonobject.tobean (jsonobj,student.class); Wu //System.out.println (Stu.getname ()); - //            } About //        } $     } -  -     Private Static voidTestjsonattribute () { -         /** A * Create a JSON object and set properties for the object +          */ theJsonobject jsonobj =NewJsonobject (); -Jsonobj.put ("Int_att", 25);//adding an int type property $Jsonobj.put ("String_att", "str");//Add string Property theJsonobj.put ("Double_att", 12.25);//Add double Type property theJsonobj.put ("Boolean_att",true);//Adding a Boolean property the         //Adding a Jsonobject Type property theJsonobject Jsonobjson =NewJsonobject (); -Jsonobjson.put ("id", 1); inJsonobjson.put ("name", "Tom"); theJsonobj.put ("Jsonobject_att", Jsonobjson); the         //Adding a Jsonarray Type property AboutJsonarray Jsonarray =NewJsonarray (); theJsonarray.add ("Array0"); theJsonarray.add ("Array1"); theJsonarray.add ("Array2"); +Jsonarray.add ("Array3"); -Jsonobj.put ("Jsonarray_att", Jsonarray); the System.out.println (jsonobj.tostring ());BayiSystem.out.println ("Int_att:" +jsonobj.getint ("Int_att")); theSystem.out.println ("String_att:" +jsonobj.getstring ("String_att")); theSystem.out.println ("Double_att:" +jsonobj.getdouble ("Double_att")); -System.out.println ("Boolean_att:" +jsonobj.getboolean ("Boolean_att")); -System.out.println ("Jsonobject_att:" +jsonobj.getjsonobject ("Jsonobject_att")); theSystem.out.println ("Jsonarray_att:" +jsonobj.getjsonarray ("Jsonarray_att")); the     } the  the     /** - * Java objects and JSON objects are converted to each other the      */ the     Private Static voidTestjsonbean () { the         /**94 * Create Java Objects the          */ theStudent Student =NewStudent (); theStudent.setid (1);98Student.setname ("Jag"); AboutStudent.setsex ("Man"); -Student.setage (25);101Student.sethobby (Newstring[]{"basketball", "surfing the Internet", "Running", "games"});102         /**103 * Java objects are converted to JSON objects and get JSON object properties104          */ theJsonobject Jsonstu =Jsonobject.fromobject (student);106System.out.println ("bean-106:" +jsonstu.tostring ());107System.out.println ("bean-107:" +jsonstu.getjsonarray ("hobby"));108         /**109 * JSON objects are converted to Java objects and get Java object Properties the          */111Student stu = (Student) Jsonobject.tobean (Jsonstu, Student.class); the System.out.println (Stu.getname ());113         /** the * Create JSON object the          */ theJsonobject jsonobj =NewJsonobject ();117Jsonobj.put ("id", 1);118Jsonobj.put ("name", "Zhang Yong");119Jsonobj.put ("Sex", "male"); -Jsonobj.put ("Age", 24);121         //jsonobj.put ("hobby", New string[]{"Internet", "games", "Running", "Music"});122         //System.out.println (jsonobj.tostring ());123         /**124 * JSON objects are converted to Java objects the          */126Student stud = (Student) Jsonobject.tobean (jsonobj,student.class);127 System.out.println (Stud.getname ()); -     }129}

Converting between Java objects and JSON objects

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.