Organize Java JSON tools

Source: Internet
Author: User

1. Configure the development and running environment

JSON required package

Commons-httpclient-3.1.jar
Commons-lang-2.4.jar
Commons-logging-1.1.1.jar
Json-lib-2.2.3-jdk13.jar
Ezmorph-1.0.6.jar
Commons-collections-3.2.1.jar

Xom-1.0b3.jar

Commons-beanutils.jar

The above package can be

Http://commons.apache.org/index.html

Http://json-lib.sourceforge.net/

Http://ezmorph.sourceforge.net/

Http://morph.sourceforge.net/

Http://www.docjar.com/

  1. Package com. Alibaba yyy. polabs. util. JSON;
  2. Import java. Text. parseexception;
  3. Import java. util. arraylist;
  4. Import java. util. date;
  5. Import java. util. hashmap;
  6. Import java. util. iterator;
  7. Import java. util. List;
  8. Import java. util. Map;
  9. Import com. Alibaba yyy. polabs. util. dateutil;
  10. Import net. SF. JSON. jsonarray;
  11. Import net. SF. JSON. jsonobject;
  12. Import net. SF. JSON. jsonconfig;
  13. Import net. SF. JSON. util. cycledetectionstrategy;
  14. /**
  15. * JSON tool class, which converts JSON data into Java objects and Java objects into JSON
  16. Public class jsonutil {
  17. /**
  18. * Get a Java object from the character format of a JSON object
  19. *
  20. * @ Param jsonstring
  21. * @ Param pojocalss
  22. * @ Return
  23. */
  24. Public static object getobject4jsonstring (string jsonstring, class pojocalss ){
  25. Object pojo;
  26. Jsonobject = jsonobject. fromobject (jsonstring );
  27. Pojo = jsonobject. tobean (jsonobject, pojocalss );
  28. Return pojo;
  29. }
  30. /**
  31. * Obtain a map from the JSON hash expression and modify map to support nesting.
  32. *
  33. * @ Param jsonstring
  34. * @ Return
  35. */
  36. Public static map getmap4json (string jsonstring ){
  37. Jsonobject = jsonobject. fromobject (jsonstring );
  38. Iterator keyiter = jsonobject. Keys ();
  39. String key;
  40. Object value;
  41. Map valuemap = new hashmap ();
  42. While (keyiter. hasnext ()){
  43. Key = (string) keyiter. Next ();
  44. Value = jsonobject. Get (key );
  45. Valuemap. Put (Key, value );
  46. }
  47. Return valuemap;
  48. }
  49. /**
  50. * Get the corresponding Java array from the json Array
  51. *
  52. * @ Param jsonstring
  53. * @ Return
  54. */
  55. Public static object [] getobjectarray4json (string jsonstring ){
  56. Jsonarray = jsonarray. fromobject (jsonstring );
  57. Return jsonarray. toarray ();
  58. }
  59. /**
  60. * Get a Java object list from the JSON object set expression
  61. *
  62. * @ Param jsonstring
  63. * @ Param pojoclass
  64. * @ Return
  65. */
  66. Public static list getlist4json (string jsonstring, class pojoclass ){
  67. Jsonarray = jsonarray. fromobject (jsonstring );
  68. Jsonobject;
  69. Object pojovalue;
  70. List list = new arraylist ();
  71. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  72. Jsonobject = jsonarray. getjsonobject (I );
  73. Pojovalue = jsonobject. tobean (jsonobject, pojoclass );
  74. List. Add (pojovalue );
  75. }
  76. Return list;
  77. }
  78. /**
  79. * Parse the Java String Array from the JSON Array
  80. *
  81. * @ Param jsonstring
  82. * @ Return
  83. */
  84. Public static string [] getstringarray4json (string jsonstring ){
  85. Jsonarray = jsonarray. fromobject (jsonstring );
  86. String [] stringarray = new string [jsonarray. Size ()];
  87. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  88. Stringarray [I] = jsonarray. getstring (I );
  89. }
  90. Return stringarray;
  91. }
  92. /**
  93. * Parse the javalong object array from the json Array
  94. *
  95. * @ Param jsonstring
  96. * @ Return
  97. */
  98. Public static long [] getlongarray4json (string jsonstring ){
  99. Jsonarray = jsonarray. fromobject (jsonstring );
  100. Long [] longarray = new long [jsonarray. Size ()];
  101. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  102. Longarray [I] = jsonarray. getlong (I );
  103. }
  104. Return longarray;
  105. }
  106. /**
  107. * Parse the Java integer object array from the json Array
  108. *
  109. * @ Param jsonstring
  110. * @ Return
  111. */
  112. Public static integer [] getintegerarray4json (string jsonstring ){
  113. Jsonarray = jsonarray. fromobject (jsonstring );
  114. Integer [] integerarray = new integer [jsonarray. Size ()];
  115. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  116. Integerarray [I] = jsonarray. getint (I );
  117. }
  118. Return integerarray;
  119. }
  120. /**
  121. * Parse the Java date object array from the json array. This method must ensure that
  122. *
  123. * @ Param jsonstring
  124. * @ Return
  125. * @ Throws parseexception
  126. */
  127. Public static date [] getdatearray4json (string jsonstring, string dataformat)
  128. Throws parseexception {
  129. Jsonarray = jsonarray. fromobject (jsonstring );
  130. Date [] datearray = new date [jsonarray. Size ()];
  131. String datestring;
  132. Date;
  133. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  134. Datestring = jsonarray. getstring (I );
  135. Date = dateutil. parsedate (datestring, dataformat );
  136. Datearray [I] = date;
  137. }
  138. Return datearray;
  139. }
  140. /**
  141. * Parse the Java integer object array from the json Array
  142. *
  143. * @ Param jsonstring
  144. * @ Return
  145. */
  146. Public static double [] getdoublearray4json (string jsonstring ){
  147. Jsonarray = jsonarray. fromobject (jsonstring );
  148. Double [] doublearray = new double [jsonarray. Size ()];
  149. For (INT I = 0; I <jsonarray. Size (); I ++ ){
  150. Doublearray [I] = jsonarray. getdouble (I );
  151. }
  152. Return doublearray;
  153. }
  154. /**
  155. * Convert a Java object to a JSON string
  156. *
  157. * @ Param javaobj
  158. * @ Return
  159. */
  160. Public static string getjsonstring4javapojo (Object javaobj ){
  161. Jsonobject JSON;
  162. JSON = jsonobject. fromobject (javaobj );
  163. Return JSON. tostring ();
  164. }
  165. }
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.