Android JsonUtil

2021-01-14  本文已影响0人  ryanxun
嘿,今天的你过的还好吗,今天分享JsonUtil
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

/**
 *  基于FastJson封装Json
 */
public class JsonUtil {
    private static SerializeConfig config;
    private static SerializerFeature[] features = {
            //输出空值字段
            SerializerFeature.WriteMapNullValue,
            //如果数组结果为null,则输出为[],而不是null
            SerializerFeature.WriteNullListAsEmpty,
            //数值字段为null,则输出为0,而不是null
            SerializerFeature.WriteNullNumberAsZero,
            //Boolean字段为null,则输出为false,而不是null
            SerializerFeature.WriteNullBooleanAsFalse,
            //字符类型如果为null,则输出为" ",而不是null
            SerializerFeature.WriteNullStringAsEmpty
    };
    static {
        config = new SerializeConfig();
        //使用json-lib兼容的日期输出格式
        config.put(java.util.Date.class,new JSONLibDataFormatSerializer());
    }

    /**
     * 将一个对象装换为Json字符串
     */
    public static String toJSONString(Object object) {
        return JSONObject.toJSONString(object,config,features);
    }

    /**
     * 将Json字符串转换为Object类型的
     * */
    public  static  Object toObject(String str){
        return JSON.parse(str);
    }

    /**
     * 将Json字符串转换为实例
     * */
    public static <T> T toObject(String str,Class<T> t){
        return JSON.parseObject(str,t);
    }

    /**
     * 将Json转换为指定类型的List
     * */
    public static <T> List<T> toList(String str, Class<T> t){
        return JSON.parseArray(str,t);
    }

    /**
     * 将Json转换为Map
     * */
    public static <T> Map<String,T> toMap(String str){
        return (Map<String, T>) JSONObject.parseObject(str);
    }
}

没更新动态或者频繁更新动态的时候都是在认真生活

上一篇 下一篇

猜你喜欢

热点阅读