• Android Studio 自动生成 Gson bean


    很多时候, 把对象转为 Json 或是把 Json 转为对象时, 需要创建 Gson Bean 的实体类, 但一个一个创建会很麻烦, 所以有时候需要借助一些工具。

    博主博客

    一、使用 Android Studio 的插件

    1. 打开 Settings,选择菜单栏 File -> Settings…
    2. 左边选择 Plugins 选项,右边进入之后选择 Browse repositories…
    3. 搜索 GsonFormat,选中之后选择右边 Install 安装插件。
    4. 安装完成后, 重启 Android Studio
    5. 首先创建一个 bean, 然后使用快捷键 Alt + S, 打开插件 GUI 之后, 在里面粘贴 json 格式字符串, 选择 ok
    6. 在弹出的窗口中, 选择需要生成的字段, 再选择 ok, 即可自动生成需要的 Gson bean

    二、使用第三方网站

    使用 jsonschema2pojo 把 json 粘贴上去后, 填好需要生成的类的包名、类名、数据源、注解类型, 生成方法等, 即可在线预览或是下载 Zip 包。

    jsonschema2pojo教程
    如上图所示, 把需要转成 beanjson 粘贴到文本框中。 然后

    • Package 填写要生成类的包名
    • Class name 填写生成类的类名
    • Source type 选择 JSON
    • Annotation style 如果使用 Gson 框架解析 json 可以在这里勾选 Gson, 用其他库就选择其他的, 会自动生成对应注解。

    剩下的根据自己需求进行填写。然后可以通过点击 Preview 可以预览生成的类。

    比如上面的 json 会生成

    -----------------------------------com.example.Bar.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("jsonschema2pojo")
    public class Bar {
    
    @SerializedName("type")
    @Expose
    private String type;
    
    /**
    * No args constructor for use in serialization
    *
    */
    public Bar() {
    }
    
    /**
    *
    * @param type
    */
    public Bar(String type) {
    super();
    this.type = type;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    }
    -----------------------------------com.example.Baz.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("jsonschema2pojo")
    public class Baz {
    
    @SerializedName("type")
    @Expose
    private String type;
    
    /**
    * No args constructor for use in serialization
    *
    */
    public Baz() {
    }
    
    /**
    *
    * @param type
    */
    public Baz(String type) {
    super();
    this.type = type;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    }
    -----------------------------------com.example.Example.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("jsonschema2pojo")
    public class Example {
    
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("properties")
    @Expose
    private Properties properties;
    
    /**
    * No args constructor for use in serialization
    *
    */
    public Example() {
    }
    
    /**
    *
    * @param type
    * @param properties
    */
    public Example(String type, Properties properties) {
    super();
    this.type = type;
    this.properties = properties;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    public Properties getProperties() {
    return properties;
    }
    
    public void setProperties(Properties properties) {
    this.properties = properties;
    }
    
    }
    -----------------------------------com.example.Foo.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("jsonschema2pojo")
    public class Foo {
    
    @SerializedName("type")
    @Expose
    private String type;
    
    /**
    * No args constructor for use in serialization
    *
    */
    public Foo() {
    }
    
    /**
    *
    * @param type
    */
    public Foo(String type) {
    super();
    this.type = type;
    }
    
    public String getType() {
    return type;
    }
    
    public void setType(String type) {
    this.type = type;
    }
    
    }
    -----------------------------------com.example.Properties.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("jsonschema2pojo")
    public class Properties {
    
    @SerializedName("foo")
    @Expose
    private Foo foo;
    @SerializedName("bar")
    @Expose
    private Bar bar;
    @SerializedName("baz")
    @Expose
    private Baz baz;
    
    /**
    * No args constructor for use in serialization
    *
    */
    public Properties() {
    }
    
    /**
    *
    * @param bar
    * @param foo
    * @param baz
    */
    public Properties(Foo foo, Bar bar, Baz baz) {
    super();
    this.foo = foo;
    this.bar = bar;
    this.baz = baz;
    }
    
    public Foo getFoo() {
    return foo;
    }
    
    public void setFoo(Foo foo) {
    this.foo = foo;
    }
    
    public Bar getBar() {
    return bar;
    }
    
    public void setBar(Bar bar) {
    this.bar = bar;
    }
    
    public Baz getBaz() {
    return baz;
    }
    
    public void setBaz(Baz baz) {
    this.baz = baz;
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239

    如果觉得没问题, 可以点击 Zip 下载该类。

  • 相关阅读:
    优先级队列【堆】——数据结构
    【软考软件评测师】第二十九章 可靠性可用性测试
    course-nlp——2-svd-nmf-topic-modeling
    手撸spring05: 解析数据源 加入事务管理 封装连接数据库 并解析返回结果集代理映射
    java中的IO缓冲流(高效流)---原始流的升级版
    超详细的hadoop完全分布式安装及xsync等各个脚本
    离散数学---判断矩阵:自反性,反自反性,对称性得到矩阵的自反闭包,对称闭包。
    (附源码)ssm小米购物网站 毕业设计 261624
    分布式微服务 - 3.降级熔断 - 3.Sentinel
    洛谷P1024 [NOIP2001 提高组] 一元三次方程求解(优雅的暴力+二分,干净利落)
  • 原文地址:https://blog.csdn.net/dxk539687357/article/details/126754394