Converting between Java objects and JSON objects

Source: Internet
Author: User

[Java]View Plaincopy
  1. Import java.util.ArrayList;
  2. Import java.util.Collection;
  3. Import Java.util.Iterator;
  4. Import java.util.List;
  5. Import Net.sf.json.JSONArray;
  6. Import Net.sf.json.JSONObject;
  7. Public class MainClass {
  8. public static void Main (string[] args) {
  9. Testjsonbean ();
  10. Testjsonattribute ();
  11. Testjsonarray ();
  12. }
  13. @SuppressWarnings ("Rawtypes")
  14. private static void Testjsonarray () {
  15. Student student1 = new Student ();
  16. Student1.setid (1);
  17. Student1.setname ("Jag");
  18. Student1.setsex ("man");
  19. Student1.setage (25);
  20. Student1.sethobby (new string[]{"basketball","Game"});
  21. Student Student2 = new Student ();
  22. Student2.setid (2);
  23. Student2.setname ("Tom");
  24. Student2.setsex ("woman");
  25. Student2.setage (23);
  26. Student2.sethobby (new string[]{"surfing","Running"});
  27. list<student> list = new arraylist<student> ();
  28. List.add (STUDENT1);
  29. List.add (Student2);
  30. Jsonarray Jsonarray = jsonarray.fromobject (list);
  31. System.out.println (Jsonarray.tostring ());
  32. Jsonarray New_jsonarray=jsonarray.fromobject (Jsonarray.toarray ());
  33. Collection java_collection=jsonarray.tocollection (New_jsonarray);
  34. if (java_collection!=null &&!java_collection.isempty ())
  35. {
  36. Iterator It=java_collection.iterator ();
  37. While (It.hasnext ())
  38. {
  39. Jsonobject Jsonobj=jsonobject.fromobject (It.next ());
  40. Student stu= (Student) Jsonobject.tobean (jsonobj,student.   Class);
  41. System.out.println (Stu.getname ());
  42. }
  43. }
  44. }
  45. private static void Testjsonattribute () {
  46. /** 
  47. * Create a JSON object and set properties for the object
  48. */
  49. Jsonobject jsonobj = new Jsonobject ();
  50. Jsonobj.put ("Int_att"); Adding an int type property
  51. Jsonobj.put ("String_att","str"); Add string property
  52. Jsonobj.put ("Double_att",12.25); Add double Type property
  53. Jsonobj.put ("Boolean_att",true); Adding a Boolean property
  54. //Add Jsonobject type Properties
  55. Jsonobject Jsonobjson = new Jsonobject ();
  56. Jsonobjson.put ("id", 1);
  57. Jsonobjson.put ("name", "Tom");
  58. Jsonobj.put ("Jsonobject_att", Jsonobjson);
  59. //Add Jsonarray type Properties
  60. Jsonarray Jsonarray = new Jsonarray ();
  61. Jsonarray.add ("array0");
  62. Jsonarray.add ("array1");
  63. Jsonarray.add ("array2");
  64. Jsonarray.add ("array3");
  65. Jsonobj.put ("Jsonarray_att", Jsonarray);
  66. System.out.println (Jsonobj.tostring ());
  67. System.out.println ("Int_att:" +jsonobj.getint ("Int_att"));
  68. System.out.println ("String_att:" +jsonobj.getstring ("String_att"));
  69. System.out.println ("Double_att:" +jsonobj.getdouble ("Double_att"));
  70. System.out.println ("Boolean_att:" +jsonobj.getboolean ("Boolean_att"));
  71. System.out.println ("Jsonobject_att:" +jsonobj.getjsonobject ("Jsonobject_att"));
  72. System.out.println ("Jsonarray_att:" +jsonobj.getjsonarray ("Jsonarray_att"));
  73. }
  74. /** 
  75. * Java objects and JSON objects are converted to each other
  76. */
  77. private static void Testjsonbean () {
  78. /** 
  79. * Create Java objects
  80. */
  81. Student Student = new Student ();
  82. Student.setid (1);
  83. Student.setname ("Jag");
  84. Student.setsex ("man");
  85. Student.setage (25);
  86. Student.sethobby (new string[]{"basketball","Internet","Running", "Game"});
  87. /** 
  88. * Java objects are converted to JSON objects and get JSON object properties
  89. */
  90. Jsonobject Jsonstu = jsonobject.fromobject (student);
  91. System.out.println (Jsonstu.tostring ());
  92. System.out.println (Jsonstu.getjsonarray ("hobby"));
  93. /** 
  94. * JSON objects are converted to Java objects and get Java object properties
  95. */
  96. Student stu = (Student) Jsonobject.tobean (Jsonstu, Student.   Class);
  97. System.out.println (Stu.getname ());
  98. /** 
  99. * Create JSON Object
  100. */
  101. Jsonobject jsonobj = new Jsonobject ();
  102. Jsonobj.put ("id",1);
  103. Jsonobj.put ("name","Zhang Yong");
  104. Jsonobj.put ("Sex","male");
  105. Jsonobj.put ("Age",24);
  106. //jsonobj.put ("hobby", New string[]{"Internet", "games", "Running", "Music"});
  107. //system.out.println (jsonobj.tostring ());
  108. /** 
  109. * JSON objects are converted to Java objects
  110. */
  111. Student stud = (Student) Jsonobject.tobean (jsonobj,student.   Class);
  112. System.out.println (Stud.getname ());
  113. }
  114. }

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.