ca478b939ce87c9910a39422736f40219bb9b991.svn-base 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.chinacreator.videoalliance.order.util;
  2. import java.lang.reflect.Type;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.google.gson.Gson;
  8. import com.google.gson.GsonBuilder;
  9. import com.google.gson.JsonDeserializationContext;
  10. import com.google.gson.JsonDeserializer;
  11. import com.google.gson.JsonElement;
  12. import com.google.gson.JsonParseException;
  13. import com.google.gson.JsonPrimitive;
  14. import com.google.gson.JsonSerializationContext;
  15. import com.google.gson.JsonSerializer;
  16. public class JsonUtil {
  17. private static Gson gson = null;
  18. static {
  19. if (gson == null) {
  20. gson = new Gson();
  21. }
  22. }
  23. private JsonUtil() {
  24. }
  25. /**
  26. * 将对象转换成json格式
  27. *
  28. * @param ts
  29. * @return
  30. */
  31. public static String objectToJson(Object ts) {
  32. String jsonStr = null;
  33. if (gson != null) {
  34. jsonStr = gson.toJson(ts);
  35. }
  36. return jsonStr;
  37. }
  38. /**
  39. * 将对象转换成json格式(并自定义日期格式)
  40. *
  41. * @param ts
  42. * @return
  43. */
  44. public static String objectToJsonDateSerializer(Object ts,
  45. final String dateformat) {
  46. String jsonStr = null;
  47. gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new JsonSerializer<Date>() {
  48. public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
  49. SimpleDateFormat format = new SimpleDateFormat(dateformat);
  50. return new JsonPrimitive(format.format(src));
  51. }
  52. }).setDateFormat(dateformat).create();
  53. if (gson != null) {
  54. jsonStr = gson.toJson(ts);
  55. }
  56. return jsonStr;
  57. }
  58. /**
  59. * 将json格式转换成list对象
  60. *
  61. * @param jsonStr
  62. * @return
  63. */
  64. public static List<?> jsonToList(String jsonStr) {
  65. List<?> objList = null;
  66. if (gson != null) {
  67. java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<?>>() {
  68. }.getType();
  69. objList = gson.fromJson(jsonStr, type);
  70. }
  71. return objList;
  72. }
  73. /**
  74. * 将json格式转换成list对象,并准确指定类型
  75. * @param jsonStr
  76. * @param type
  77. * @return
  78. */
  79. public static List<?> jsonToList(String jsonStr, java.lang.reflect.Type type) {
  80. List<?> objList = null;
  81. if (gson != null) {
  82. objList = gson.fromJson(jsonStr, type);
  83. }
  84. return objList;
  85. }
  86. /**
  87. * 将json格式转换成map对象
  88. *
  89. * @param jsonStr
  90. * @return
  91. */
  92. public static Map<?, ?> jsonToMap(String jsonStr) {
  93. Map<?, ?> objMap = null;
  94. if (gson != null) {
  95. java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<?, ?>>() {}.getType();
  96. objMap = gson.fromJson(jsonStr, type);
  97. }
  98. return objMap;
  99. }
  100. /**
  101. * 将json转换成bean对象
  102. *
  103. * @param jsonStr
  104. * @return
  105. */
  106. public static Object jsonToBean(String jsonStr, Class<?> cl) {
  107. Object obj = null;
  108. if (gson != null) {
  109. // java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<cl>() {}.getType();
  110. obj = gson.fromJson(jsonStr, cl);
  111. }
  112. return obj;
  113. }
  114. /**
  115. * 将json转换成bean对象
  116. *
  117. * @param jsonStr
  118. * @param cl
  119. * @return
  120. */
  121. @SuppressWarnings("unchecked")
  122. public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern) {
  123. Object obj = null;
  124. gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
  125. public Date deserialize(JsonElement json, Type typeOfT,JsonDeserializationContext context)
  126. throws JsonParseException {
  127. SimpleDateFormat format = new SimpleDateFormat(pattern);
  128. String dateStr = json.getAsString();
  129. try {
  130. return format.parse(dateStr);
  131. } catch (java.text.ParseException e) {
  132. // TODO Auto-generated catch block
  133. e.printStackTrace();
  134. }
  135. return null;
  136. }
  137. }).setDateFormat(pattern).create();
  138. if (gson != null) {
  139. obj = gson.fromJson(jsonStr, cl);
  140. }
  141. return (T) obj;
  142. }
  143. /**
  144. * 根据
  145. *
  146. * @param jsonStr
  147. * @param key
  148. * @return
  149. */
  150. public static Object getJsonValue(String jsonStr, String key) {
  151. Object rulsObj = null;
  152. Map<?, ?> rulsMap = jsonToMap(jsonStr);
  153. if (rulsMap != null && rulsMap.size() > 0) {
  154. rulsObj = rulsMap.get(key);
  155. }
  156. return rulsObj;
  157. }
  158. }