123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package com.imed.costaccount.common.util;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.serializer.SerializeConfig;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import java.util.List;
- import java.util.Map;
- /**
- * @Auther: mukehua
- * @Date: 2018/10/18 10:20
- * @Description:
- */
- public final class JsonUtil {
- private static final SerializeConfig config;
- static {
- config = new SerializeConfig();
- // config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
- // config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
- }
- private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
- SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
- SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
- SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
- SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
- };
- /**
- * 将对象转为JSON字符串
- * @param object
- * @return
- */
- public static String toJSONString(Object object) {
- return JSON.toJSONString(object, config, features);
- }
- public static String toJSONStringNoFeatures(Object object) {
- return JSON.toJSONString(object, config);
- }
- public static Object toBean(String text) {
- return JSON.parse(text);
- }
- /**
- * 将字符串转成指定类型bean
- * @param text
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> T toBean(String text, Class<T> clazz) {
- return JSON.parseObject(text, clazz);
- }
- // 转换为数组
- public static Object[] toArray(String text) {
- return toArray(text, null);
- }
- /**
- * 将字符串转成指定类型数组
- * @param text
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> Object[] toArray(String text, Class<T> clazz) {
- return JSON.parseArray(text, clazz).toArray();
- }
- /**
- * 将字符串转成指定类型集合
- * @param text
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> List<T> toList(String text, Class<T> clazz) {
- return JSON.parseArray(text, clazz);
- }
- // /**
- // * 将javabean转化为序列化的json字符串
- // * @param keyvalue
- // * @return
- // */
- /*public static Object beanToJson(KeyValue keyvalue) {
- String textJson = JSON.toJSONString(keyvalue);
- Object objectJson = JSON.parse(textJson);
- return objectJson;
- }*/
- /**
- * 将string转化为序列化的json字符串
- * @param text
- * @return
- */
- public static Object textToJson(String text) {
- Object objectJson = JSON.parse(text);
- return objectJson;
- }
- /**
- * json字符串转化为map
- * @param s
- * @return
- */
- public static <K, V> Map<K, V> stringToCollect(String s) {
- Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
- return m;
- }
- /**
- * 转换JSON字符串为对象
- * @param jsonData
- * @param clazz
- * @return
- */
- public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
- return JSONObject.parseObject(jsonData, clazz);
- }
- public static Object convertJSONToObject(String content, Class<?> clazz) {
- return JSONObject.parseObject(content, clazz);
- }
- /**
- * 将map转化为string
- * @param m
- * @return
- */
- public static <K, V> String collectToString(Map<K, V> m) {
- String s = JSONObject.toJSONString(m);
- return s;
- }
- }
|