byte json 互转_json与Ojbect互相转换

这篇博客介绍了如何使用Jackson库在Java中实现JSON字符串与Map对象之间的互相转换。提供了一系列静态方法,如getString、getList、getMap等,用于从Map中获取不同类型的值,同时提供了将对象转化为JSON字符串的函数。
摘要由CSDN通过智能技术生成

json与Ojbect互相转换,用到的第三方库为:jackson-mapper-asl-*.jar

import java.io.IOException;

import java.math.BigDecimal;

import java.text.DateFormat;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.JsonParseException;

import org.codehaus.jackson.map.JsonMappingException;

public class JsonUtil {

/**

*

* 此方法描述的是:根据key取得相应的值

*

* @param map

* 欲取值的map

* @param key

* key

* @return String

* @throws IOException

* @throws JsonMappingException

* @throws JsonGenerationException

*/

public static String getString(Map map, String key) throws JsonGenerationException,

JsonMappingException, IOException {

try {

return (String) map.get(key);

}

catch (Exception e) {

return JsonUtil.toString(map.get(key));

}

}

/**

*

* 此方法描述的是:取得list

*

* @param map

* 欲取值的map

* @param key

* key

* @return List>

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*/

@SuppressWarnings("unchecked")

public static List> getList(Map map, String key) throws JsonParseException,

JsonMappingException, IOException {

if (null == map) {

return null;

}

try {

return (List>) map.get(key);

}

catch (Exception e) {

return JsonUtil.toList(map.get(key));

}

}

/**

*

* 此方法描述的是:取得list

*

* @param map

* 欲取值的map

* @param key

* key

* @return Map

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*/

@SuppressWarnings("unchecked")

public static Map getMap(Map map, String key) throws JsonParseException,

JsonMappingException, IOException {

try {

return (Map) map.get(key);

}

catch (Exception e) {

return JsonUtil.toBean(map.get(key), Map.class);

}

}

/**

*

* 此方法描述的是:根据key取值int

*

* @param map

* 欲取值的map

* @param key

* key

* @param defaultValue

* 默认值

* @return int

*/

public static int getInt(Map map, String key, int defaultValue) {

try {

return (Integer) map.get(key);

}

catch (Exception e) {

try {

return Integer.parseInt(JsonUtil.toString(map.get(key)));

}

catch (Exception e2) {

return defaultValue;

}

}

}

/**

*

* 此方法描述的是:根据key取BigDecial

*

* @param map

* 欲取值的map

* @param key

* key

* @param defaultValue

* 默认值

* @return BigDecimal

*/

public static BigDecimal getBigDecimal(Map map, String key, BigDecimal defaultValue) {

return new BigDecimal(getDouble(map, key, defaultValue.doubleValue()));

}

/**

*

* 此方法描述的是:根据key取BigDecial

*

* @param map

* 欲取值的map

* @param key

* key

* @return BigDecimal

*/

public static BigDecimal getBigDecimal(Map map, String key) {

return new BigDecimal(getDouble(map, key));

}

/**

*

* 此方法描述的是:根据key取值int

*

* @param map

* 欲取值的map

* @param key

* key

* @return int

*/

public static int getInt(Map map, String key) {

return JsonUtil.getInt(map, key, 0);

}

/**

*

* 此方法描述的是:根据key取得boolean值

*

* @param map

* 欲取值的map

* @param key

* key

* @param defaultValue

* 默认值

* @return boolean

*/

public static boolean getBoolean(Map map, String key, boolean defaultValue) {

try {

return (Boolean) map.get(key);

}

catch (Exception e) {

try {

return Boolean.parseBoolean(JsonUtil.toString(map.get(key)));

}

catch (Exception e2) {

return defaultValue;

}

}

}

/**

*

* 此方法描述的是:根据key取得boolean值,默认为false

*

* @param map

* 欲取值的map

* @param key

* key

* @return boolean

*/

public static boolean getBoolean(Map map, String key) {

return getBoolean(map, key, false);

}

/**

*

* 此方法描述的是:向obj数组中加新元素,

*

* @param list

* list

* @param obj

* 增加的元素

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*/

@SuppressWarnings("unchecked")

public static void add(List> list, Object obj) throws JsonParseException,

JsonMappingException, IOException {

list.add(JsonUtil.toBean(list, HashMap.class));

}

/**

*

* 此方法描述的是:根据key取得double值

*

* @param map

* 欲取值的map

* @param key

* key

* @param defaultValue

* 默认值

* @return double

*/

public static double getDouble(Map map, String key, double defaultValue) {

try {

return (Double) map.get(key);

}

catch (Exception e) {

try {

return Double.parseDouble(JsonUtil.toString(map.get(key)));

}

catch (Exception e2) {

return defaultValue;

}

}

}

/**

*

* 此方法描述的是:根据key取得double值

*

* @param map

* 欲取值的map

* @param key

* key

* @return double

*/

public static double getDouble(Map map, String key) {

return getDouble(map, key, 0D);

}

/**

*

* 此方法描述的是:根据key取得double值

*

* @param map

* 欲取值的map

* @param key

* key

* @param defaultValue

* 默认值

* @return long

*/

public static long getLong(Map map, String key, long defaultValue) {

try {

return (Long) map.get(key);

}

catch (Exception e) {

try {

return Long.parseLong(JsonUtil.toString(map.get(key)));

}

catch (Exception e2) {

return defaultValue;

}

}

}

/**

*

* 此方法描述的是:根据key取得double值

*

* @param map

* 欲取值的map

* @param key

* key

* @return long

*/

public static long getLong(Map map, String key) {

return getLong(map, key, 0L);

}

/**

*

* 此方法描述的是:将Object转化为Json格式字符串

*

* @param obj

* 欲转换的对象

* @return String

* @throws IOException

* @throws JsonMappingException

* @throws JsonGenerationException

*/

public static String toString(Object obj) throws JsonGenerationException, JsonMappingException, IOException {

if (obj instanceof String) {

return (String) obj;

}

else if (null == obj) {

return null;

}

return JsonFormatter.toJsonString(obj);

}

/**

*

* 此方法描述的是:将Object转化为Json格式字符串

*

* @param obj

* 欲转换的对象

* @param dateFormat

* 日期format

* @return String

* @throws IOException

* @throws JsonMappingException

* @throws JsonGenerationException

*/

public static String toString(Object obj, DateFormat dateFormat) throws JsonGenerationException,

JsonMappingException, IOException {

if (obj instanceof String) {

return (String) obj;

}

else if (null == obj) {

return null;

}

JsonFormatter.setDateFormat(dateFormat);

return JsonFormatter.toJsonString(obj);

}

/**

*

* 此方法描述的是:将传入的对象转换成指定的对象

*

* @param

* 模板类

* @param cls

* 与转化的类

* @param obj

* 被转换的对象

* @return T

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*/

public static T toBean(Object obj, Class cls) throws JsonParseException, JsonMappingException, IOException {

if (null == obj) {

return null;

}

return JsonFormatter.toObject(JsonUtil.toString(obj), cls);

}

/**

*

* 此方法描述的是:字符串转换为List

*

* @param obj

* 与转换的对象

* @return List>

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*/

@SuppressWarnings("unchecked")

public static List> toList(Object obj) throws JsonParseException, JsonMappingException,

IOException {

List> lists = new LinkedList>();

List list = JsonUtil.toBean(obj, List.class);

if (null != list) {

for (Object object : list) {

Map map = JsonUtil.toBean(object, HashMap.class);

if (null != map) {

lists.add(map);

}

}

}

return lists;

}

/**

*

* 此方法描述的是:字符串转换为List

*

* @param

* 模板类

* @param cls

* 与转化的类

* @param obj

* 被转换的对象

* @return T

* @throws IOException

* @throws JsonMappingException

* @throws JsonParseException

*

*/

@SuppressWarnings("unchecked")

public static List toList(Object obj, Class cls) throws JsonParseException, JsonMappingException,

IOException {

List lists = new LinkedList();

List list = JsonUtil.toBean(obj, List.class);

if (null != list) {

for (Object object : list) {

T t = JsonUtil.toBean(object, cls);

if (null != t) {

lists.add(t);

}

}

}

return lists;

}

}

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

import java.net.URL;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.JsonParseException;

import org.codehaus.jackson.map.DeserializationConfig;

import org.codehaus.jackson.map.JsonMappingException;

import org.codehaus.jackson.map.ObjectMapper;

import org.codehaus.jackson.map.SerializationConfig;

import org.codehaus.jackson.map.annotate.JsonSerialize;

@SuppressWarnings({ "unchecked", "rawtypes" })

public class JsonFormatter {

private static final ThreadLocal INCLUDE_NULL_MAPPER = new ThreadLocal();

private static final ThreadLocal NOT_INCLUDE_NULL_MAPPER = new ThreadLocal();

private static ObjectMapper getMapper(boolean serializeNull) {

ThreadLocal tl = serializeNull ? INCLUDE_NULL_MAPPER : NOT_INCLUDE_NULL_MAPPER;

if (null == tl.get()) {

ObjectMapper mapper = new ObjectMapper();

mapper.disable(new DeserializationConfig.Feature[] { DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES });

mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

if (!serializeNull) {

mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

mapper.disable(new SerializationConfig.Feature[] { SerializationConfig.Feature.WRITE_NULL_MAP_VALUES });

}

tl.set(mapper);

}

return (ObjectMapper) tl.get();

}

public static String toJsonString(Object obj) throws JsonGenerationException, JsonMappingException, IOException {

return toJsonString(obj, true);

}

public static String toJsonAsString(Object obj) throws JsonGenerationException, JsonMappingException, IOException {

return toJsonAsString(obj, true);

}

public static byte[] toJsonAsBytes(Object obj) throws JsonGenerationException, JsonMappingException, IOException {

return toJsonAsBytes(obj, true);

}

public static void toJsonToFile(File file, Object obj) throws JsonGenerationException, JsonMappingException,

IOException {

toJsonToFile(file, obj, true);

}

public static void toJsonToOutputStream(OutputStream out, Object obj) throws JsonGenerationException,

JsonMappingException, IOException {

toJsonToOutputStream(out, obj, true);

}

public static void toJsonToWriter(Writer writer, Object obj) throws JsonGenerationException, JsonMappingException,

IOException {

toJsonToWriter(writer, obj, true);

}

public static T toObject(String json, Class clazz) throws JsonParseException, JsonMappingException,

IOException {

return toObject(json, clazz, true);

}

public static T toObject(byte[] src, Class clazz) throws JsonParseException, JsonMappingException,

IOException {

return toObject(src, clazz, true);

}

public static T toObject(File file, Class clazz) throws JsonParseException, JsonMappingException,

IOException {

return toObject(file, clazz, true);

}

public static T toObject(InputStream input, Class clazz) throws JsonParseException, JsonMappingException,

IOException {

return toObject(input, clazz, true);

}

public static T toObject(Reader reader, Class clazz) throws JsonParseException, JsonMappingException,

IOException {

return toObject(reader, clazz, true);

}

public static T toObject(URL url, Class clazz) throws JsonParseException, JsonMappingException, IOException {

return toObject(url, clazz, true);

}

public static String toJsonString(Object obj, boolean serializeNull) throws JsonGenerationException,

JsonMappingException, IOException {

return getMapper(serializeNull).writeValueAsString(obj);

}

public static String toJsonAsString(Object obj, boolean serializeNull) throws JsonGenerationException,

JsonMappingException, IOException {

return getMapper(serializeNull).writeValueAsString(obj);

}

public static byte[] toJsonAsBytes(Object obj, boolean serializeNull) throws JsonGenerationException,

JsonMappingException, IOException {

return getMapper(serializeNull).writeValueAsBytes(obj);

}

public static void toJsonToFile(File file, Object obj, boolean serializeNull) throws JsonGenerationException,

JsonMappingException, IOException {

getMapper(serializeNull).writeValue(file, obj);

}

public static void toJsonToOutputStream(OutputStream out, Object obj, boolean serializeNull)

throws JsonGenerationException, JsonMappingException, IOException {

getMapper(serializeNull).writeValue(out, obj);

}

public static void toJsonToWriter(Writer writer, Object obj, boolean serializeNull) throws JsonGenerationException,

JsonMappingException, IOException {

getMapper(serializeNull).writeValue(writer, obj);

}

public static T toObject(String json, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(json, clazz);

}

public static T toObject(byte[] src, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(src, clazz);

}

public static T toObject(File file, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(file, clazz);

}

public static T toObject(InputStream input, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(input, clazz);

}

public static T toObject(Reader reader, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(reader, clazz);

}

public static T toObject(URL url, Class clazz, boolean serializeNull) throws JsonParseException,

JsonMappingException, IOException {

return getMapper(serializeNull).readValue(url, clazz);

}

public static void setDateFormat(DateFormat dateFormat) {

getMapper(true).setDateFormat(dateFormat);

getMapper(false).setDateFormat(dateFormat);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值