Object type recording turn Map

  /**
     *  将json,转为map
     * */
    public static Map returnMapByObject(Object object){
        Map maps = (Map) JSON.parse(returnJsonByObject(object));
        return maps;
    }

/**
     *  利用 Gson 转换 object(Entity) 序列化为json,字符串
     * */
    public static String returnJsonByObject(Object object){
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        String jsonStr=gson.toJson(object);
        return jsonStr;
    }

In the project, we met the needs of the Map Object object into a scene, so I quoted the above method for testing. The following picture is the Object object

 

 

 After starting to write the time, did not notice the time formatting issues, then using the above Gson turn into a map, directly became "Nov 28, 2019 4:00:36 PM" This string format, which makes me very troubled ah, because to perform sql operation, I could not find a way to turn this into a timestamp type

So with the following code

public static Map<String,Object> Obj2Map(Object obj) throws Exception{
        Map<String,Object> map=new HashMap<String, Object>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }

With this, I can be in the screenshot above time into "2019-11-28T06: 52: 09.724 + 0000" format, although the format looks far less common, but still can handle.

Treatment is a common format, I see another essay.

 https://www.cnblogs.com/justtodo/p/11979815.html

Guess you like

Origin www.cnblogs.com/justtodo/p/11979729.html