String,对象,Map,json互转封装类

来源:互联网 发布:js cookie 跨域 编辑:程序博客网 时间:2024/06/06 17:34
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;


import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * josn处理类
 * @author
 * @date
 */
public class JsonUtil {
private static final Logger logger = Logger.getLogger(JsonUtil.class);
private final static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
/**
* 格式化对象为json字符串
* @param obj
* @return
* @throws Exception
* @author
* @throws JsonProcessingExceptio
* @date
*/
public static String formatObjectToJson(Object obj) throws JsonProcessingException{
String result = null;
logger.debug("【JsonUtil】格式化Object为string字符串操作");
result = objectMapper.writeValueAsString(obj);
return result;
}
/**
* 格式化对象为byte[]
* @param obj
* @return
* @throws Exception
* @author
* @throws JsonProcessingException 
* @date
*/
public static byte[] formatObjectToBytes(Object obj) throws JsonProcessingException{
byte[] result = null;
logger.debug("【JsonUtil】格式化Object为string字符串操作");
result = objectMapper.writeValueAsBytes(obj);
return result;
}

/**
* 格式化输入参数对象的方法
* @param data json字符串
* @return ParamInfo
* @throws JsonParseException json转换异常
* @throws JsonMappingException json映射异常
* @throws IOException io异常
* @author
* @date
*/
public static ParamInfo formatDataToBean(String data) throws JsonParseException, JsonMappingException, IOException{
logger.debug("【JsonUtil】格式化string为ParamInfo操作");
ParamInfo paramInfo = objectMapper.readValue(data, ParamInfo.class);
return paramInfo;
}

/**
* 转换json字符串为Java对象
* @param data json字符串
* @return Object
* @throws JsonParseException json转换异常
* @throws JsonMappingException json映射异常
* @throws IOException io异常
* @author 
* @throws IOException 
* @throws JsonMappingException 
* @throws JsonParseException 
* @dte 
*/
public static Object formatDataStrToObjectBean(String data,Class<?> c) throws JsonParseException, JsonMappingException, IOException {
logger.debug("【JsonUtil】格式化string为JAVA BEAN 操作");
return objectMapper.readValue(data, c);
}


/**
* 格式化参数为map对象的方法
* @param data
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
* @author
* @date
*/
@SuppressWarnings("unchecked")
public static Map<String,Object> formatDataToMap(String data) throws JsonParseException, JsonMappingException, IOException{
logger.debug("【JsonUtil】格式化string为Map操作");
Map<String,Object> maps = objectMapper.readValue(data, Map.class);
return maps;
}



/**
* 转换json为List<T> 对象
* @param data json数据
* @param clazz 需要转换的类
* @return <T> List<Object>
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
* @author
* @date
*/
public static <T> List<T> formatDataToList(String data,Class<?> clazz) throws JsonParseException, JsonMappingException, IOException{
logger.debug("【JsonUtil】格式化string为List<Object>操作");
JavaType javaType  = objectMapper.getTypeFactory().constructParametricType(List.class, clazz);
List<T> list = objectMapper.readValue(data, javaType);
return list;
}


/**
* 格式化返回字符串json
* @param code 请求的参数
* @param errorCode 错误码
* @return String
* @author 
* @date 
*/
public static String formatResult(String code,String errorCode,String info){
String result = "{\"rst\":\"{RESULT}\",\"msg\":\"{MESSAGE}\",\"data\":\"\",\"code\":\"{CODE}\",\"date\":\"{DATE}\",\"info\":\"{INFO}\"}";
result = result.replace("{RESULT}", MessageCode.RESULT_FALSE);
result = result.replace("{MESSAGE}", errorCode);
result = result.replace("{CODE}", code);
result = result.replace("{DATE}", DateUtil.getDateString());
result = result.replace("{INFO}", info);
return result;
}


/**
* @param args
* @author 
* @date
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
String data = FileUtils.readFileToString(new File("D://apply.txt"));

String str = JsonUtil.formatObjectToJson(data);

System.out.println(str);
JavaType javaType  = objectMapper.getTypeFactory().constructParametricType(List.class, Map.class);
List<Object> list = objectMapper.readValue(data, javaType);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

原创粉丝点击