JSON转换总是日期总是变成当前系统时间分析

最近很不顺,恶心的问题接踵而至,好像恶魔缠身,这不,我保存日期到数据库,前几天好好的,后来具体不行了,老是变成当期日期,

哦哦,有时候居然变好了,一会又不行了。


想一想,行的时候干什么了,这或许是解决问题的关键,墨迹了数个小时,才找到问题,气的我无语了。

    public static Object json2Object(String jsonString, Class pojoClass) {
         JSONObject jsonObject = JSONObject.fromObject(jsonString);
         String[] dateFormats = new String[] {"yyyy-MM-dd"};  
         JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));  
         Map classMap = new HashMap();
         return JSONObject.toBean(jsonObject,pojoClass,classMap);  
    }
      public static List>json2List(String jsonString){
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        List>list = new ArrayList>();
        for (int i = 0; i < jsonArray.size(); i++){
          Mapmap = json2Map(jsonArray.getJSONObject(i).toString());
          list.add(map);
        }
        return list;
      }

注意,标注颜色的代码,之所以有时候可以,因为有时候先运行了这个方法,在SONUtils中注入了正确的日期格式,也就是yyyy-MM-dd

哦哦,那为什么有时候不可以呢,

    public static  List json2List(String jsonString, Class pojoClass) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        List list = new ArrayList();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            T pojoValue = (T)JSONObject.toBean(jsonObject, pojoClass);
            list.add(pojoValue);
        }
        return list;
      }

你瞧瞧,这个方法没有注入这个日期格式,如果先条用这个,我传的日期格式JSONUtils.这货可能就分析错误了,直接把我弄成当前时间了


好吧 问题总算找到了,我就来个静态块,类加载的时候就注入下,一次性搞定

   
    static{
         String[] dateFormats = new String[] {"yyyy-MM-dd"};  
         JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));  
    }
   
这样,这个奇葩怪异的问题被我墨迹墨迹的解决了,让问题再来的猛烈点吧,还不信治不了你们了

你可能感兴趣的:(Web前端设计,程序异常)