• java对象的序列化反序列化


    (1)JAVA对象序列化方法

        /**
         * 将JAVA对象序列化为JSON字符串
         */
        public String serializeJson(Object obj) throws IOException {
            String _json = new Gson().toJson(obj);
            return _json;
        }

    (2)JAVA对象反序列化方法

        /**
         * 将JSON字符串反序列化为JAVA对象
         */
        public <T> T deserializeJson(String json, Class<T> cla) throws JsonParseException, IOException {
            Gson gson = new Gson();
            return gson.fromJson(json, cla);
        }

    (3)反序列化List<Object>

        /**
         * 将JSON字符串反序列化为JAVA对象集合
         * @param <T>
         */
        public <T> List<T>  deserializeListJson(String json, Class<T> cla) throws JsonParseException, IOException {
            Gson gson = new Gson();
            @SuppressWarnings({ "unchecked", "rawtypes" })
            List<LinkedTreeMap> list = gson.fromJson(json, List.class);
            List<T> result = new ArrayList<>();
            if(list!=null && list.size()>0){
                for (@SuppressWarnings("rawtypes") LinkedTreeMap item : list) {
                    result.add(JsonUtils.getInstance().deserializeJson(JsonUtils.getInstance().serializeJson(item),cla));
                }
            }
            return result;
        }

    之前一直使用的(2)中的方法,但是那样反序列化出来的List对象在遍历的时候会出错:

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.google.gson.JsonParseException;
    import com.googosoft.model.MessageBeans;
    import com.googosoft.util.JsonUtils;
    
    /**
     * @author songyan
     * @date 2020年6月3日 上午8:06:40
     * @desc 序列化反序列化测试类
     */
    public class JsonTest {
    
        public static void main(String[] args) throws JsonParseException, IOException {
            //封装测试数据
            List<MessageBeans> messageBeansList = new ArrayList<>();
            for (int i = 0; i <3; i++) {
                MessageBeans messageBeans = new MessageBeans();
                messageBeans.setCommand("command"+(i+1));
                messageBeansList.add(messageBeans);
            }
            
            //序列化反序列化
            JsonUtils jsonUtils = JsonUtils.getInstance();
            @SuppressWarnings("unchecked")
            List<MessageBeans> list = jsonUtils.deserializeJson(jsonUtils.serializeJson(messageBeansList), List.class);
            for (MessageBeans messageBeans : list) {
                System.out.println(messageBeans);
            }
        }
        
    }

     反序列化List<Object>对象的正确姿势:

            //序列化反序列化
            JsonUtils jsonUtils = JsonUtils.getInstance();
            List<MessageBeans> list = jsonUtils.deserializeListJson(jsonUtils.serializeJson(messageBeansList), MessageBeans.class);
            for (MessageBeans messageBeans : list) {
                System.out.println(messageBeans);
            }

    关于LinkedTreeMap:https://www.jianshu.com/p/bd650eb62ca5

  • 相关阅读:
    在xcode5中修改整个项目名
    如何调试堆损坏
    My Life with Isaac Stern by Aaron Rosand
    Seagate 硬盘产地查询
    服务器返回 HTTP 500
    Exception code: 0xE0434352
    When curl sends 100-continue
    Milestone 不能卸载,修复 Counter 即可
    GE 遇到的 UAC 导致不能自动启动的问题
    关于 UAC,Mark Russinovich on the Future of Security
  • 原文地址:https://www.cnblogs.com/excellencesy/p/13035201.html
Copyright © 2020-2023  润新知