对象转json

// 涉及jar包
jackson-core-2.6.1
jackson-databind-2.6.1
jackson-annotations-2.6.0

import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ReferenceType;

/**
 * 描述:
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2018年4月9日      01371247         Create
 * ****************************************************************************
 * </pre>
 * @author 01371247
 * @since 1.0
 */
public class JSONUtils {

	private static final Logger logger = LoggerFactory.getLogger(JSONUtils.class);

	private JSONUtils() {
	}

	public static JSONObject parseJSONObject(String json) {
		try {
			return new JSONObject(json);
		} catch (Exception e) {
			logger.error("parse JSONObject error!", e);
		}
		return null;
	}

	public static JSONArray parseJSONArray(String jsonArray) {
		try {
			return new JSONArray(jsonArray);
		} catch (Exception e) {
			logger.error("parse parseJSONArray error!", e);
		}
		return null;
	}

	public static String getString(JSONObject obj, String name) {
		String value = "";
		try {
			value = obj.getString(name);
		} catch (Exception e) {
			logger.error("JSONObject get String value error!", e);
		}
		return value;
	}

	public static Long getLong(JSONObject obj, String name) {
		Long value = 0l;
		try {
			value = obj.getLong(name);
		} catch (Exception e) {
			logger.error("JSONObject get Long value error!", e);
		}
		return value;
	}

	public static JSONArray getJSONArray(JSONObject obj, String name) {
		try {
			return obj.getJSONArray(name);
		} catch (Exception e) {
			logger.error("JSONObject get JSONArray value error!{}", e.getMessage());
		}
		return null;
	}

	public static String toJson(Object obj) {
		if (obj == null) {
			return "";
		}
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			logger.error("JSONUtils to String error!", e);
		}
		return "";
	}

	public static <T> T fromJson(String json, Class<T> clazz) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			return mapper.readValue(json, clazz);
		} catch (IOException e) {
			logger.error("JSONUtils fromJson error!", e);
		}
		return null;
	}
	public static <T> T fromJson(String json, TypeReference<T> typeReference) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.readValue(json, typeReference);
		} catch (IOException e) {
			logger.error("JSONUtils fromJson error!", e);
		}
		return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值