json工具類JsonUtil
阿新 • • 發佈:2019-01-09
package com.lgt.util; 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; import com.safein.webservice.page.ParamInfo; import com.safein.webservice.util.MessageCode; /** * josn處理類 */ 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 julong * @throws JsonProcessingException * @date 2016-6-23 上午11:23:56 */ 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 * @throws JsonProcessingException */ 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異常 */ 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異常 * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ 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 */ @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 */ 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 */ 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 */ 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(); } // String str = "{\"rst\":null,\"msg\":null,\"data\":null,\"code\":null,\"date\":null,\"page\":null,\"user\":null}"; // System.out.println(str); // List<UserInfo> userList = new ArrayList<UserInfo>(); // for (int i = 0; i < 6; i++) { // UserInfo user = new UserInfo(); // user.setUserId("AAA"+i); // userList.add(user); // } // try { // String data = JsonUtil.formatObjectToJson(userList); // System.out.println(data); // // try { // ParamInfo paramInfo1 = JsonUtil.formatDataToBean(str); // //格式化為json // String json = JsonUtil.formatObjectToJson(paramInfo1.getData()); // //json 格式化為map // Map<String,Object> maps = JsonUtil.formatDataToMap(json); // //Map<String, Object> map = JsonUtil.formatDataToMap(null); // System.out.println("---------------"+maps); // List<UserInfo> list = JsonUtil.formatDataToList(data, UserInfo.class); // for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i).getUserId()); // } // // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } catch (JsonProcessingException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // UserInfo userInfo = new UserInfo(); // System.out.println(JsonUtil.formatResult("1", "123", "1234")); } }