JSON处理类

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * 
 * <b>类说明:</b>Jackson工具类
 * 
 * <p>
 * <b>详细描述:</b>
 * 
 * @author liuhuanchao
 * @since  2016-04-18
 */
public class JacksonUtil {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    private static final JsonFactory JSONFACTORY = new JsonFactory();

    /**
     * 转换Java Bean 为 json
     */
    public static String beanToJson(Object o) throws JsonParseException {
        StringWriter sw = new StringWriter();
        JsonGenerator jsonGenerator = null;

        try {
            jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
            MAPPER.writeValue(jsonGenerator, o);
            return sw.toString();

        } catch (Exception e) {
            throw new RuntimeException(e+"转换Java Bean 为 json错误");

        } finally {
            if (jsonGenerator != null) {
                try {
                    jsonGenerator.close();
                } catch (Exception e) {
                    throw new RuntimeException(e+"转换Java Bean 为 json错误");
                }
            }
        }
    }

    /**
     * json 转 javabean
     *
     * @param json
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object jsonToBean(String json, Class clazz) throws JsonParseException {
        try {
//        	MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        	
            return MAPPER.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e+"json 转 javabean错误");
        }
    }

    /**
     * 转换Java Bean 为 HashMap
     */
    @SuppressWarnings("unchecked")
	public static Map<String, Object> beanToMap(Object o) throws JsonParseException {
        try {
            return MAPPER.readValue(beanToJson(o), HashMap.class);
        } catch (Exception e) {
            throw new RuntimeException(e+"转换Java Bean 为 HashMap错误");
        }
    }

    /**
     * 转换Json String 为 HashMap
     */
    @SuppressWarnings("unchecked")
	public static Map<String, Object> jsonToMap(String json, boolean collToString) throws JsonParseException {
        Map<String, Object> map = null;
        try {
            map = MAPPER.readValue(json, HashMap.class);
        } catch (IOException e) {
            throw new RuntimeException(e+"转换Java Bean 为 HashMap错误");
        }
        if (collToString) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof Collection || entry.getValue() instanceof Map) {
                    entry.setValue(beanToJson(entry.getValue()));
                }
            }
        }
        return map;

    }

    /**
     * List 转换成json
     *
     * @param list
     * @return
     */
    public static String listToJson(List<Map<String, String>> list) throws JsonParseException {
        JsonGenerator jsonGenerator = null;
        StringWriter sw = new StringWriter();
        try {
            jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
            new ObjectMapper().writeValue(jsonGenerator, list);
            jsonGenerator.flush();
            return sw.toString();
        } catch (Exception e) {
            throw new RuntimeException(e+"List 转换成json错误");
        } finally {
            if (jsonGenerator != null) {
                try {
                    jsonGenerator.flush();
                    jsonGenerator.close();
                } catch (Exception e) {
                    throw new RuntimeException(e+"List 转换成json错误");
                }
            }
        }
    }

    /**
     * json 转List
     *
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List<Map<String, String>> jsonToList(String json) throws JsonParseException {
        try {
            if (json != null && !"".equals(json.trim())) {
                JsonParser jsonParse = JSONFACTORY.createJsonParser(new StringReader(json));

                return (List<Map<String, String>>) new ObjectMapper().readValue(jsonParse, ArrayList.class);
            } else {
                throw new RuntimeException("json 转List错误");
            }
        } catch (Exception e) {
            throw new RuntimeException(e+"json 转List错误");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值