加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONUtils工具类

发布时间:2020-12-16 18:46:56 所属栏目:百科 来源:网络整理
导读:package com.admin.util;import java.io.IOException;import java.text.SimpleDateFormat;import org.apache.commons.lang3.StringUtils;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.JsonProcessingException;
package com.admin.util;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json工具类
 * 
 * @author chengdaolu
 * @date 2017年5月28日
 */
public class JsonUtil {

	private static final ObjectMapper json = new ObjectMapper()
			.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

	public String toString(Object obj) throws JsonProcessingException {

		return obj == null ? "" : json.writeValueAsString(obj);

	}

	public static <T> T toObj(String str,Class<T> valueType)
			throws JsonParseException,JsonMappingException,IOException {
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		return json.readValue(str,valueType);
	}

	public static <T> T toList(String str,TypeReference<T> valueTypeRef)
			throws JsonParseException,IOException {
		if (StringUtils.isEmpty(str) || null == valueTypeRef) {
			return null;
		}
		return json.readValue(str,valueTypeRef);
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读