JsonUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.imed.costaccount.common.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializeConfig;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import java.util.List;
  7. import java.util.Map;
  8. /**
  9. * @Auther: mukehua
  10. * @Date: 2018/10/18 10:20
  11. * @Description:
  12. */
  13. public final class JsonUtil {
  14. private static final SerializeConfig config;
  15. static {
  16. config = new SerializeConfig();
  17. // config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
  18. // config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
  19. }
  20. private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
  21. SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
  22. SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
  23. SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
  24. SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
  25. };
  26. /**
  27. * 将对象转为JSON字符串
  28. * @param object
  29. * @return
  30. */
  31. public static String toJSONString(Object object) {
  32. return JSON.toJSONString(object, config, features);
  33. }
  34. public static String toJSONStringNoFeatures(Object object) {
  35. return JSON.toJSONString(object, config);
  36. }
  37. public static Object toBean(String text) {
  38. return JSON.parse(text);
  39. }
  40. /**
  41. * 将字符串转成指定类型bean
  42. * @param text
  43. * @param clazz
  44. * @param <T>
  45. * @return
  46. */
  47. public static <T> T toBean(String text, Class<T> clazz) {
  48. return JSON.parseObject(text, clazz);
  49. }
  50. // 转换为数组
  51. public static Object[] toArray(String text) {
  52. return toArray(text, null);
  53. }
  54. /**
  55. * 将字符串转成指定类型数组
  56. * @param text
  57. * @param clazz
  58. * @param <T>
  59. * @return
  60. */
  61. public static <T> Object[] toArray(String text, Class<T> clazz) {
  62. return JSON.parseArray(text, clazz).toArray();
  63. }
  64. /**
  65. * 将字符串转成指定类型集合
  66. * @param text
  67. * @param clazz
  68. * @param <T>
  69. * @return
  70. */
  71. public static <T> List<T> toList(String text, Class<T> clazz) {
  72. return JSON.parseArray(text, clazz);
  73. }
  74. // /**
  75. // * 将javabean转化为序列化的json字符串
  76. // * @param keyvalue
  77. // * @return
  78. // */
  79. /*public static Object beanToJson(KeyValue keyvalue) {
  80. String textJson = JSON.toJSONString(keyvalue);
  81. Object objectJson = JSON.parse(textJson);
  82. return objectJson;
  83. }*/
  84. /**
  85. * 将string转化为序列化的json字符串
  86. * @param text
  87. * @return
  88. */
  89. public static Object textToJson(String text) {
  90. Object objectJson = JSON.parse(text);
  91. return objectJson;
  92. }
  93. /**
  94. * json字符串转化为map
  95. * @param s
  96. * @return
  97. */
  98. public static <K, V> Map<K, V> stringToCollect(String s) {
  99. Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
  100. return m;
  101. }
  102. /**
  103. * 转换JSON字符串为对象
  104. * @param jsonData
  105. * @param clazz
  106. * @return
  107. */
  108. public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
  109. return JSONObject.parseObject(jsonData, clazz);
  110. }
  111. public static Object convertJSONToObject(String content, Class<?> clazz) {
  112. return JSONObject.parseObject(content, clazz);
  113. }
  114. /**
  115. * 将map转化为string
  116. * @param m
  117. * @return
  118. */
  119. public static <K, V> String collectToString(Map<K, V> m) {
  120. String s = JSONObject.toJSONString(m);
  121. return s;
  122. }
  123. }