• Gson实例


        @Test
        public void testObj2Json(){
            // 定义对象
            Person person = new Person();
            person.setId(1);
            person.setName("jim");
            person.setAge(24);
            person.setMale(true);
            person.setBirthday(new Date());
            
            // 实例化Gson并使用方法toJson(Object src)转json串
            Gson gson = new Gson();
            String jsonStr = gson.toJson(person);
            
            // 输出
            System.out.println(jsonStr);
        }
        
        /**
         * Gson节点对象:
         *     JsonElement:所有的节点 都是JsonElement对象.
         *     JsonPrimitive:基本数据类型的节点对象,JsonElement的子类.
         *     JsonNull:空节点对象,JsonElement的子类.
         *     JsonObject : 对象节点对象, JsonElement的 子类.
         *     JsonArray : 数组节点对象, JsonElement的子类.
         * JsonElement的取值:
         *     JsonPrimitive:value的 取值对应 java的int,double,float,long,short,boolean,char,byte,String,BigDecimal,BigInteger,Number
         *     JsonObject:value的取值对应 java的Object对象.
         *     JsonArray:value的取值对应java的List及其子类对象.
         */
        @Test
        public void testJson2Obj(){
            // {"id":1,"name":"jim","age":24,"isMale":true,"birthday":"Sep 6, 2015 6:06:34 AM"}
            String json = "{"id":1,"name":"jim","age":24,"isMale":true,"birthday":"Sep 6, 2015 6:06:34 AM"}";
            
            // 实例化Gson实例,并 使用 fromJson(String json, Class<Person> classOfT)来转成对象
            Gson gson = new Gson();
            Person p = gson.fromJson(json, Person.class);
            
            // 输出
            System.out.println(p);
        }
        
        @Test
        public void testJson2List(){
            String json = "[{"id":1,"name":"jim","age":24,"isMale":true,"birthday":"Sep 6, 2015 6:06:34 AM"},{"id":2,"name":"jack","age":25,"isMale":true,"birthday":"Sep 3, 2015 9:06:34 PM"}]";
            
            // 实例化Gson实例,并 使用 fromJson(String json, Class<Person> classOfT)来转成对象
            // 使用 TypeToken 来封装List类型
            Gson gson = new Gson();
            List<Person> ps = gson.fromJson(json, new TypeToken<List<Person>>(){}.getType());
            
            for (Person person : ps) {
                System.out.println(person);
            }
        }
        
        @Test
        public void testJson2Map(){
            String json = "{"p1":{"id":1,"name":"jim","age":24,"isMale":true,"birthday":"Sep 6, 2015 6:06:34 AM"},"p2":{"id":2,"name":"jack","age":25,"isMale":true,"birthday":"Sep 3, 2015 9:06:34 PM"}}";
            
            // 实例化Gson实例,并 使用 fromJson(String json, Class<Person> classOfT)来转成对象
            // 使用 TypeToken 来封装Map类型
            Gson gson = new Gson();
            Map<String,Person> pmap = gson.fromJson(json, new TypeToken<Map<String,Person>>(){}.getType());
            
            // 迭代输出
            for (Iterator<String> iter = pmap.keySet().iterator();iter.hasNext();) {
                String key = iter.next();
                Person val = pmap.get(key);
                
                System.out.println(key + ":" + val);
            }
        }
        
        @Test
        public void testParseJson(){
            String json = "{"p1":{"id":1,"name":"jim","age":24,"isMale":true,"birthday":"Sep 6, 2015 6:06:34 AM"},"p2":{"id":2,"name":"jack","age":25,"isMale":true,"birthday":"Sep 3, 2015 9:06:34 PM"}}";
            
            // Json 解析对象,先获取整个json串
            JsonParser jp = new JsonParser();
            JsonElement element = jp.parse(json);
            
            // 整个是一个对象,因此先获取Object,在从Object中获取Object(key=p1)
            JsonObject jobj = element.getAsJsonObject();
            JsonObject p1 = jobj.getAsJsonObject("p1");
            
            // 获取到的Object在转换成Person对象
            Person p = new Gson().fromJson(p1, Person.class);
            System.out.println(p);
        }

    代码: https://github.com/cbooy/GsonDemo

  • 相关阅读:
    C++学习之路(四):线程安全的单例模式
    C++学习之路(三):volatile关键字
    C++学习之路(五):复制构造函数与赋值运算符重载
    类对象作为函数参数进行值传递
    System V共享内存介绍
    关于迭代器失效
    C++学习之路(二):引用
    C++学习之路(一):const与define,结构体对齐,new/delete
    epoll内核源码分析
    Redux中间件之redux-thunk使用详解
  • 原文地址:https://www.cnblogs.com/cbooy/p/4784358.html
Copyright © 2020-2023  润新知