很多时候, 把对象转为 Json 或是把 Json 转为对象时, 需要创建 Gson Bean 的实体类, 但一个一个创建会很麻烦, 所以有时候需要借助一些工具。
使用 jsonschema2pojo 把 json 粘贴上去后, 填好需要生成的类的包名、类名、数据源、注解类型, 生成方法等, 即可在线预览或是下载 Zip 包。
如上图所示, 把需要转成 bean
的 json
粘贴到文本框中。 然后
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;
}
}
如果觉得没问题, 可以点击 Zip
下载该类。