Java中关于Json格式转化Object,Map,Collection类型和String类型之间的转化在我们实际项目中应用的很是普遍和广泛。最近工作的过程中也是经常有,因此,自己封装了一个JsonUtil.java类。
所需要的jar包:slf4j-jdk14-1.5.6.jar,slf4j-api-1.5.6.jar------>此两个jar包是slf4j日志jar包
jackson-annotations-2.1.1.jar,jackson-core-2.1.1.jar,jackson-databind-2.1.1.jar---->实现Json转化的依赖包。
--------------------------------------分割线 --------------------------------------
在C语言中解析JSON配置文件
--------------------------------------分割线 --------------------------------------
package com.clark.util;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.clark.model.User;
/*
*
* @author caolipeng
* @title JsonUtil.java
* @description 提供用于Json数据转换的静态方法
* @version V1.0
*/
public class JsonUtil {
private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
/**
* 性能方面 JackSon > Gson > Json 故这里决定使用JackSon 将Collection转换为Json
* 参数是Collection
*
* @param collection
*/
public static String toJson(Collection<Object> collection) {
/*
* 使用Gson的方式 Gson gson = new Gson(); String json = gson.toJson(map);
*/
/*
* 使用Jackson的方式
*/
String json = "[]";
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
try {
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, collection);
gen.close();
json = sw.toString();
} catch (JsonGenerationException e) {
json = "[]";
logger.error(
"------1------ toJson JsonGenerationException error:\n", e);
} catch (JsonMappingException e) {
json = "[]";
logger.error("------2------ toJson JsonMappingException error:\n",
e);
} catch (IOException e) {
json = "[]";
logger.error("------3------ toJson IOException error:\n", e);
}
return json;
}
/**
* @Title: toJson
* @Description: 将对象转换为Json数据
* @param @param obj
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String toJson(Object obj) {
String json = "[]";
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
try {
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, obj);
gen.close();
json = sw.toString();
} catch (JsonGenerationException e) {
json = "[]";
logger.error("------1------ toJson IOException error:\n", e);
} catch (JsonMappingException e) {
json = "[]";
logger.error("------2------ toJson IOException error:\n", e);
} catch (IOException e) {
json = "[]";
logger.error("------3------ toJson IOException error:\n", e);
}
return json;
}