How to serialize a JSON object child into a field?

前端 未结 6 841
一生所求
一生所求 2021-01-19 10:22

I have a JSON object that looks like this

{
  \"foo\":{
      \"bar\":\"bar\",
      \"echo\":\"echo\"
  }
}

But then my Java object looks

6条回答
  •  梦毁少年i
    2021-01-19 10:41

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("foo")
    @Expose
    private Foo foo;
    
    public Foo getFoo() {
    return foo;
    }
    
    public void setFoo(Foo foo) {
    this.foo = foo;
    }
    
    }
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Foo {
    
    @SerializedName("bar")
    @Expose
    private String bar;
    @SerializedName("echo")
    @Expose
    private String echo;
    
    public String getBar() {
    return bar;
    }
    
    public void setBar(String bar) {
    this.bar = bar;
    }
    
    public String getEcho() {
    return echo;
    }
    
    public void setEcho(String echo) {
    this.echo = echo;
    }
    
    }
    

    you can find more details here

提交回复
热议问题