在平常工做中,發現這麼一個小問題,作後臺開發,給app返回的一個實體類,屬性是有大小寫區分的。但實際返回的時候卻全都是小寫的。而給PC返回的時候就都是有大小寫區分的。java
這個是spring跳轉的時候跟自動轉換了。spring
咱們可使用一套json的工具類,進行強制轉換json
package com.minxin.me.backstage.commons; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import net.sf.ezmorph.object.DateMorpher; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.CycleDetectionStrategy; import net.sf.json.util.JSONUtils; /** * <p>Title: Json工具類</p> */ public class JsonUtil{ public static void renderJSON(HttpServletResponse res, String s) { render(res, s, "application/json;charset=utf-8"); } private static void render(HttpServletResponse res, String s, String contextType) { try { res.setHeader("Pragma", "no-cache"); res.setHeader("Cache-Control", "no-cache"); res.setDateHeader("Expires", 0); res.setContentType(contextType); res.getWriter().write(s); } catch (IOException e) { e.printStackTrace(); } finally { try { res.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } } } /** * * 將java對象轉換成json字符串 * * @param javaObj * * @return */ public static String getJsonString4JavaPOJO(Object javaObj) { JSONObject json; json = JSONObject.fromObject(javaObj); return json.toString(); } /** * * 將java對象轉換成json字符串,並設定日期格式 * */ public static String getJsonString4JavaPOJO(Object javaObj,String dataFormat) { JSONObject json; JsonConfig jsonConfig = configJson(dataFormat); json = JSONObject.fromObject(javaObj, jsonConfig); return json.toString(); } /** * * 將java對象轉換成json字符串,並設定日期格式 * */ public static String getJsonArrayString4JavaPOJO(Object javaObj,String dataFormat) { JSONArray json; JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss"); json = JSONArray.fromObject(javaObj, jsonConfig); return json.toString(); } @SuppressWarnings("rawtypes") public static JSONObject objectcollect2json(List list, int total) { Map<String, Object> map = new HashMap<String, Object>(); map.put("total", total); map.put("rows", list); return JSONObject.fromObject(map); } /** * * JSON 時間解析器具 * * @param datePattern * * @return */ public static JsonConfig configJson(String datePattern) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] { "" }); jsonConfig.setIgnoreDefaultExcludes(false); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor(datePattern)); return jsonConfig; } /** * * * * @param excludes * * @param datePattern * * @return */ public static JsonConfig configJson(String[] excludes, String datePattern) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(excludes); jsonConfig.setIgnoreDefaultExcludes(false); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(datePattern)); return jsonConfig; } /** * * <p> * Title: * </p> * <p> * Description: 從一個JSON 對象字符格式中獲得一個java對象 * </p> * * @author Robin * @param jsonString * @param pojoCalss * @return */ @SuppressWarnings("rawtypes") public static Object getObject4JsonString(String jsonString, Class pojoCalss) { Object pojo = null; try { JSONObject jsonObject = JSONObject.fromObject(jsonString); pojo = JSONObject.toBean(jsonObject, pojoCalss); } catch (Exception e) { e.printStackTrace(); } return pojo; } /** * 將json字符串轉換成List<T>對象,處理日期類型 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> List<T> jsonToList(String json,Class clazz,String[] datePattern,Map classMap){ JSONArray jsonArray = JSONArray.fromObject(json); JsonConfig jsonConfig = new JsonConfig();//參數設置 jsonConfig.setRootClass(clazz);//設置array中的對象類型 jsonConfig.setClassMap(classMap); String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss"}; JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats)); //將數組轉換成T類型的集合 List<T> list = (List<T>)JSONArray.toCollection(jsonArray, jsonConfig); return list; } /** * 將json字符串轉換成List<T>對象 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> List<T> jsonToList(String json,Class clazz){ JSONArray jsonArray = JSONArray.fromObject(json); JsonConfig jsonConfig = new JsonConfig();//參數設置 jsonConfig.setRootClass(clazz);//設置array中的對象類型 //將數組轉換成T類型的集合 List<T> list = (List<T>)JSONArray.toCollection(jsonArray, jsonConfig); return list; } }
@RequestMapping(value = "/fdsfr_pc") @ResponseBody public JxNewOpenCGResponse jsddregister(OpenPcCGRequest request, HttpServletResponse httpServletResponse) { logger.info("PC江西開戶開始:" + request); JxNewOpenCGResponse response = new JxNewOpenCGResponse(); // 對tokenid進行驗證 各類驗證 } logger.info("開戶響應參數:",response.toString()); JsonUtil.renderJSON(httpServletResponse, JsonUtil.getJsonString4JavaPOJO(response)); return null; }
使用上述方法,直接使用renderJSON方法,就能夠將response對象 已大小寫有區別的方式返回。數組