JSON is parsed using Net.sf.json in Java

Source: Internet
Author: User

Net.sf.json relies on a lot of packages.

There are commons-collections, Commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar

  1. /**
  2. * Get a Java object from a JSON object character format, such as:
  3. * {"id": Idvalue, "name": Namevalue, "Abean": {"Abeanid": Abeanidvalue, ...}}
  4. * @param Object
  5. * @param clazz
  6. * @return
  7. */
  8. Public Static Object getdto (String jsonstring, Class clazz) {
  9. Jsonobject jsonobject = null;
  10. Try {
  11. Setdataformat2java ();
  12. Jsonobject = Jsonobject.fromobject (jsonstring);
  13. }catch(Exception e) {
  14. E.printstacktrace ();
  15. }
  16. return Jsonobject.tobean (Jsonobject, clazz);
  17. }
  18. /**
  19. * Get a Java object from a JSON object character format, where Beanslist is a collection of a type, such as:
  20. * {"id": Idvalue, "name": Namevalue, "Abean": {"Abeanid": Abeanidvalue, ...},
  21. * beanslist:[{}, {}, ...]}
  22. * @param jsonstring
  23. * @param clazz
  24. * @param the type of the Map collection property (key: Collection property name, Value: Collection property type Class) Eg: ("beanslist": Bean.class)
  25. * @return
  26. */
  27. Public Static Object getdto (String jsonstring, Class clazz, map map) {
  28. Jsonobject jsonobject = null;
  29. Try {
  30. Setdataformat2java ();
  31. Jsonobject = Jsonobject.fromobject (jsonstring);
  32. }catch(Exception e) {
  33. E.printstacktrace ();
  34. }
  35. return Jsonobject.tobean (Jsonobject, clazz, map);
  36. }
  37. /**
  38. * Get an array of Java objects from a JSON array, such as:
  39. * [{"id": Idvalue, "name": Namevalue}, {"id": Idvalue, "name": Namevalue}, ...]
  40. * @param Object
  41. * @param clazz
  42. * @return
  43. */
  44. Public Static Object[] Getdtoarray (String jsonstring, Class clazz) {
  45. Setdataformat2java ();
  46. Jsonarray array = jsonarray.fromobject (jsonstring);
  47. object[] obj = new object[array.size ()];
  48. for (int i = 0; i < array.size (); i++) {
  49. Jsonobject Jsonobject = Array.getjsonobject (i);
  50. Obj[i] = Jsonobject.tobean (Jsonobject, clazz);
  51. }
  52. return obj;
  53. }
  54. /**
  55. * Get an array of Java objects from a JSON array, such as:
  56. * [{"id": Idvalue, "name": Namevalue}, {"id": Idvalue, "name": Namevalue}, ...]
  57. * @param Object
  58. * @param clazz
  59. * @param map
  60. * @return
  61. */
  62. Public Static Object[] Getdtoarray (String jsonstring, Class clazz, map map) {
  63. Setdataformat2java ();
  64. Jsonarray array = jsonarray.fromobject (jsonstring);
  65. object[] obj = new object[array.size ()];
  66. for (int i = 0; i < array.size (); i++) {
  67. Jsonobject Jsonobject = Array.getjsonobject (i);
  68. Obj[i] = Jsonobject.tobean (Jsonobject, clazz, map);
  69. }
  70. return obj;
  71. }
  72. /**
  73. * Get a collection of Java objects from a JSON array
  74. * @param Object
  75. * @param clazz
  76. * @return
  77. */
  78. Public Static List getdtolist (String jsonstring, Class clazz) {
  79. Setdataformat2java ();
  80. Jsonarray array = jsonarray.fromobject (jsonstring);
  81. List List = new ArrayList ();
  82. for (Iterator iter = Array.iterator (); Iter.hasnext ();) {
  83. Jsonobject Jsonobject = (jsonobject) iter.next ();
  84. List.add (Jsonobject.tobean (Jsonobject, clazz));
  85. }
  86. return list;
  87. }
  88. /**
  89. * Get a collection of Java objects from a JSON array with the object containing the collection properties
  90. * @param Object
  91. * @param clazz
  92. * @param the type of the Map collection property
  93. * @return
  94. */
  95. Public Static List getdtolist (String jsonstring, Class clazz, map map) {
  96. Setdataformat2java ();
  97. Jsonarray array = jsonarray.fromobject (jsonstring);
  98. List List = new ArrayList ();
  99. for (Iterator iter = Array.iterator (); Iter.hasnext ();) {
  100. Jsonobject Jsonobject = (jsonobject) iter.next ();
  101. List.add (Jsonobject.tobean (Jsonobject, Clazz, map));
  102. }
  103. return list;
  104. }
  105. /**
  106. * Get a map from a JSON hash expression that supports nested functions
  107. * Shape: {"id": "Johncon", "name": "Xiao Qiang"}
  108. * Note that the Commons-collections version must contain ORG.APACHE.COMMONS.COLLECTIONS.MAP.MULTIKEYMAP
  109. * @param Object
  110. * @return
  111. */
  112. Public Static Map Getmapfromjson (String jsonstring) {
  113. Setdataformat2java ();
  114. Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
  115. Map map = new HashMap ();
  116. for (Iterator iter = Jsonobject.keys (); Iter.hasnext ();) {
  117. String key = (string) iter.next ();
  118. Map.put (Key, Jsonobject.get (key));
  119. }
  120. return map;
  121. }
  122. /**
  123. * Get the appropriate Java array from the JSON array
  124. * JSON-shaped like: ["123", "456"]
  125. * @param jsonstring
  126. * @return
  127. */
  128. Public Static Object[] Getobjectarrayfromjson (String jsonstring) {
  129. Jsonarray Jsonarray = Jsonarray.fromobject (jsonstring);
  130. return Jsonarray.toarray ();
  131. }
  132. /**
  133. * Convert data Objects to JSON strings
  134. * Dto Object shape: {"id": Idvalue, "name": Namevalue, ...}
  135. * Array Object shape such as: [{}, {}, {}, ...]
  136. * Map Object shape: {key1: {"id": Idvalue, "name": Namevalue, ...}, Key2: {}, ...}
  137. * @param Object
  138. * @return
  139. */
  140. Public Static String getjsonstring (Object object) throws exception{
  141. String jsonstring = null;
  142. Date Value processor
  143. Jsonconfig jsonconfig = new jsonconfig ();
  144. Jsonconfig.registerjsonvalueprocessor (java.util.Date. Class, new jsondatevalueprocessor ());
  145. if (Object! = null) {
  146. if (Object instanceof Collection | | object instanceof object[]) {
  147. jsonstring = Jsonarray.fromobject (object, Jsonconfig). toString ();
  148. }Else{
  149. jsonstring = Jsonobject.fromobject (object, Jsonconfig). toString ();
  150. }
  151. }
  152. return jsonstring = = null ? "{}": jsonstring;
  153. }

JSON is parsed using Net.sf.json in Java

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.