com.google.gson.Gson的基本json代码工具类


import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;

public class JsonUtils {

    private static Gson gson = new GsonBuilder().serializeNulls().create();
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    
    private static MflexConfigBean mflexConfigBean;
    
    private JsonUtils() {
    }

    /**
     * 将对象转换为Json字符串
     * 
     * @param <T>
     *            转换对象的类
     * @param 要转换的对象
     * @return 转换后的字符串
     */
    public static <T> String toJsonString(T json) {
        return gson.toJson(json);
    }
    
    /**
     * 将对象转换为Json字符串数组
     * 
     * @param <T>
     *            转换对象的类
     * @param 要转换的对象
     * @return 转换后的字符串
     */
    public static <T> String toJsonArray(T json) {
        return gson.toJsonTree(json).getAsJsonArray().toString();
    }

    public static <T> T fromJson(String jsonString, Class<T> classOfT) {
        return gson.fromJson(jsonString, classOfT);
    }

    public static <T> List<T> listFromJson(String jsonString, Class<T> classOfT) {
        List<T> list = new LinkedList<>();
        JsonArray jsonArray = parseString(jsonString).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            list.add(gson.fromJson(jsonElement, classOfT));
        }
        return list;
    }

    private static JsonElement parseString(String jsonString) {
        JsonParser parser = new JsonParser();
        JsonReader reader = new JsonReader(new StringReader(jsonString));
        reader.setLenient(true);
        return parser.parse(reader);
    }
    
    public static MflexConfigBean getConfigBean() throws IOException {
        if (mflexConfigBean == null ) {
            String root = System.getProperty("user.dir");
            String readFileToString = FileUtils.readFileToString(new File(root, "application-mflex.json"), StandardCharsets.UTF_8);
            
            try {
                mflexConfigBean = gson.fromJson(readFileToString, MflexConfigBean.class);
            } catch (JsonSyntaxException e) {
                logger.error("json字符串转对象失败,被转换的json字符串为:{}, 失败信息:", readFileToString, e);
            }
        }
        
        return mflexConfigBean;
    }
    
    public static void main(String[] args) throws IOException {
//    	System.out.println(getConfigBean().toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值