zoukankan      html  css  js  c++  java
  • FastJson使用

    1.导入:

    compile 'com.alibaba:fastjson:1.2.41'

    2.使用:

    public class User {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User [name=" + name + ", age=" + age + "]";
        }
    }
    
    public class UserGroup {
        private String name;
        private List<User> users = new ArrayList<>();
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<User> getUsers() {
            return users;
        }
    
        public void setUsers(List<User> users) {
            this.users = users;
        }
    
        @Override
        public String toString() {
            return "UserGroup [name=" + name + ", users=" + users + "]";
        }
    
    }
    

    代码实例:

     User guestUser = new User();
            guestUser.setName(null);
            guestUser.setAge(28);
            // 构建用户root
            User rootUser = new User();
            rootUser.setName("root");
            guestUser.setAge(35);
            // 构建用户组对象
            UserGroup group = new UserGroup();
            group.setName(null);
            group.getUsers().add(guestUser);
            group.getUsers().add(rootUser);
            // 用户组对象转JSON串
            String jsonString = JSON.toJSONString(group);
            Log.e("Demo12Activity","jsonString:" + jsonString);
            // JSON串转用户组对象
            UserGroup group2 = JSON.parseObject(jsonString, UserGroup.class);
            Log.e("Demo12Activity","group:"+group2);
    
            // 构建用户对象数组
            User[] users = new User[2];
            users[0] = guestUser;
            users[1] = rootUser;
            // 用户对象数组转JSON串
            String jsonString2 = JSON.toJSONString(users);
            System.out.println("jsonString2:" + jsonString2);
            // JSON串转用户对象列表
            List<User> users2 = JSON.parseArray(jsonString2, User.class);
            Log.e("Demo12Activity","users2:" + users2);
    
            User user1 = users2.get(0);
            Log.e("Demo12Activity","user1:" + user1.getName());
    

     运行结果:

    jsonString:{"users":[{"age":35},{"age":0,"name":"root"}]}
    
    group:UserGroup [name=null, users=[User [name=null, age=35], User [name=root, age=0]]]
    
    users2:[User [name=null, age=35], User [name=root, age=0]]
    
    user1:null
    
  • 相关阅读:
    axios使用post提交方式
    ajax请求携带cookie和自定义请求头header(跨域和同域)
    HTML <form> enctype 属性, 表单上传
    浏览器常用设计规范,字体与行高(Ant)
    关于级联选择器el-cascader的一些属性
    elemen-ui中的table表格支持行列动态改变
    websocket双向通信
    文本超出用省略号表示或是自动换行
    新建js文件中有自己编写的方法和数据,在其他.vue文件中调用
    了解WebSocket
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/7904063.html
Copyright © 2011-2022 走看看