Java Android 开发利器 JSON to JavaBean 工具来了

2025-03-10 22:56:02
152次阅读
0个评论

前沿导读

相信大家在学习鸿蒙开发过程中最痛苦的就是编写model 类 特别是那种复杂的json的时候对不对, 这时候有一个自动化的工具给你生成,model是不是很开心。我们今天要分享的就是这个工具 JSON to JavaBean 的用法

JSON to JavaBean在线工具

地址: JSON to JavaBean

image-20250310224559292

如何使用

我们把我们的json字符串复制粘贴到我们左边的输入框里面 输入我们的包名然后迪阿尼转换即可

  • 1 简单的json

{
      "msg": "获取数据成功",
      "code": 200
}

image-20250310224804180

生成后的model

package UserInfo;

import java.util.List;

public class GeneratedBean {
    private String msg;
    private Integer code;

    public GeneratedBean() {
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "GeneratedBean{" +
            "msg=" + msg +
            ", code=" + code +
            "}";
    }
}

  • 2 含有嵌套的 json

  {   "msg": "获取数据成功",
      "code": 200,
      "data":{
         "username":"高桥凉介",
  	   "password":"123456"
     }
  }

image-20250310224833421

import java.util.List;

public class GeneratedBean {
    private String msg;
    private Integer code;
    private DataBean data;

    public GeneratedBean() {
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "GeneratedBean{" +
            "msg=" + msg +
            ", code=" + code +
            ", data=" + data +
            "}";
    }

    public static class DataBean {
        private String username;
        private String password;

        public DataBean() {
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "DataBean{" +
                "username=" + username +
                ", password=" + password +
                "}";
        }
    }
}

  • 3含有数组的json

{
    "msg": "获取数据成功",
    "code": 200,
    "data": [
        {
            "id": 1,
            "logo": "https://www.itying.com/images/flutter/1.png",
            "name": "杭州蚂蚁金服信息技术有限公司  ",
            "location": "上海新浦东区",
            "type": "互联网",
            "size": "B论",
            "employee": "1000人以上",
            "hot": "资深开放产品技术工程师",
            "count": "500",
            "inc": "蚂蚁金融服务集团(以下称\"蚂蚁金服\")起步于2004年成立的支付宝2014年10月"
        },
        {
            "id": 2,
            "logo": "https://www.itying.com/images/flutter/2.png",
            "name": "百度信息技术有限公司  ",
            "location": "广州天河区",
            "type": "互联网",
            "size": "C论",
            "employee": "500人以上",
            "hot": "全栈工程师",
            "count": "1000",
            "inc": "蚂蚁金融服务集团(以下称\"蚂蚁金服\")起步于2004年成立的支付宝2014年10月"
        },
        {
            "id": 3,
            "logo": "https://www.itying.com/images/flutter/3.png",
            "name": "腾讯科有限公司  ",
            "location": "深圳南山区",
            "type": "互联网",
            "size": "D论",
            "employee": "200人以上",
            "hot": "数据挖掘工程师",
            "count": "200",
            "inc": "蚂蚁金融服务集团(以下称\"蚂蚁金服\")起步于2004年成立的支付宝2014年10月"
        },
        {
            "id": 4,
            "logo": "https://www.itying.com/images/flutter/4.png",
            "name": "字节跳动科技有限公司",
            "location": "北京海淀区",
            "type": "互联网",
            "size": "D论",
            "employee": "1500人以上",
            "hot": "资深架构师",
            "count": "1500",
            "inc": "蚂蚁金融服务集团(以下称\"蚂蚁金服\")起步于2004年成立的支付宝2014年10月"
        }
    ]
}

image-20250310224951334

生成后的model

package UserInfo;

import java.util.List;

public class GeneratedBean {
    private String msg;
    private Integer code;
    private List<DataItemBean> data;

    public GeneratedBean() {
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public List<DataItemBean> getData() {
        return data;
    }

    public void setData(List<DataItemBean> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "GeneratedBean{" +
            "msg=" + msg +
            ", code=" + code +
            ", data=" + data +
            "}";
    }

    public static class DataItemBean {
        private Integer id;
        private String logo;
        private String name;
        private String location;
        private String type;
        private String size;
        private String employee;
        private String hot;
        private String count;
        private String inc;

        public DataItemBean() {
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getLogo() {
            return logo;
        }

        public void setLogo(String logo) {
            this.logo = logo;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getSize() {
            return size;
        }

        public void setSize(String size) {
            this.size = size;
        }

        public String getEmployee() {
            return employee;
        }

        public void setEmployee(String employee) {
            this.employee = employee;
        }

        public String getHot() {
            return hot;
        }

        public void setHot(String hot) {
            this.hot = hot;
        }

        public String getCount() {
            return count;
        }

        public void setCount(String count) {
            this.count = count;
        }

        public String getInc() {
            return inc;
        }

        public void setInc(String inc) {
            this.inc = inc;
        }

        @Override
        public String toString() {
            return "DataItemBean{" +
                "id=" + id +
                ", logo=" + logo +
                ", name=" + name +
                ", location=" + location +
                ", type=" + type +
                ", size=" + size +
                ", employee=" + employee +
                ", hot=" + hot +
                ", count=" + count +
                ", inc=" + inc +
                "}";
        }
    }
}

最后总结:

我们可以看到无论哪种json格式的数据我们都可以用插件很好生成对应的model 这个对于我们平时开发Android的app 或者是Java项目可以大大节省开发的时间。在这里也要感谢群友的关注和支持,我这边后期会一步一步优化这个工具希望能更加智能和好用。有兴趣的同学没有关注坚果派官网社区。里面有很多大神分享鸿蒙相关的技术点。

团队介绍

团队介绍:坚果派由坚果等人创建,团队由12位华为HDE以及若干热爱鸿蒙的开发者和其他领域的三十余位万粉博主运营。专注于分享HarmonyOS/OpenHarmony,ArkUI-X,元服务,仓颉,团队成员聚集在北京,上海,南京,深圳,广州,宁夏等地,目前已开发鸿蒙原生应用,三方库60+,欢迎进行课程,项目等合作。

坚果派官网地址 :

www.nutpi.net/

如果需要学习更多鸿蒙的知识可以关注我B站教程

课程地址

B站课程地址:www.bilibili.com/cheese/play…

项目内容:

  • 1 常用布局组件的学习

  • 2 网络请求工具类封装

  • 3 arkui 生命周期启动流程

  • 4 日志工具类的封装

  • 5 自定义组合组件的封装

  • 6 路由导航跳转的使用

  • 7 本地地数据的缓存 以及缓存工具类的封装

  • 8 欢迎页面的实现

  • 9 登录案例和自动登录效果实现

  • 10 请求网络数据分页上拉加载 下拉刷新的实现

  • 11 list数据懒加载实现

  • 12 webview组件的使用

收藏00

登录 后评论。没有帐号? 注册 一个。