• java内部类


    1、内部静态类

    import cn.hutool.core.collection.CollUtil;
    import com.alibaba.fastjson.JSONObject;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    
    import java.util.List;
    
    /**
     * @author www.gomepay.com
     * @date 2019/12/20
     */
    @Data
    @AllArgsConstructor
    public class Dept {
        String deptName;
        List<User> list;
        @Data
        @AllArgsConstructor
        public static class User {
            String userName;
        }
    
        public static void main(String[] args) {
            List<User> list = CollUtil.newArrayList(new User("y1"),new User("y2"));
            Dept dept = new Dept("研发部",list);
            String jsonString = JSONObject.toJSONString(dept);
            Dept dept2 = JSONObject.parseObject(jsonString,Dept.class);
            System.out.println(dept2);
        }
    }

    输出:Dept(deptName=研发部, list=[Dept.User(userName=y1), Dept.User(userName=y2)])

    2、内部普通类

    import cn.hutool.core.collection.CollUtil;
    import com.alibaba.fastjson.JSONObject;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.List;
    
    /**
     * @author www.gomepay.com
     * @date 2019/12/20
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Dept2 {
        String deptName;
        List<User> list;
        @Data
        @AllArgsConstructor
        public class User {
            String userName;
        }
    
        public static void main(String[] args) {
            List<User> list = CollUtil.newArrayList(new Dept2().new User("y1"),new Dept2().new User("y2"));
            Dept2 dept = new Dept2("研发部",list);
            String jsonString = JSONObject.toJSONString(dept);
            Dept2 dept2 = JSONObject.parseObject(jsonString, Dept2.class);
            System.out.println(dept2);
        }
    }

    输出:Dept2(deptName=研发部, list=[Dept2.User(userName=y1), Dept2.User(userName=y2)])

  • 相关阅读:
    上传文件至服务器(图片)
    centos7语言更改
    centos7无法访问虚拟机web服务
    Java中四种访问权限总结
    线程和进程、程序、应用程序之间的关系
    kafka
    图文并茂理解iptables
    扩展模块
    根据子网掩码计算最大主机数
    iptables匹配条件总结1
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/12072250.html
Copyright © 2020-2023  润新知