繁体   English   中英

无法将JSON响应反序列化为Java对象

[英]Unable to deserialize JSON response into Java Objects

我知道对此主题有很多疑问,但没有任何帮助我解决以下问题

    {
  "_embedded": {
    "customers": [
      {
        "id": 101,
        "name": "John",
        "city": "Ohio"
      },
      {
        "id": 102,
        "name": "Tom",
        "city": "London"
      }
    ]
  }
}

为此,我创建了以下Java对象:

@Data
public class Wrapper {
    @JsonProperty("_embedded")
    private Customers customer;
  }

@Data
public class Customers {
@JsonProperty("customer")
private List<Foo> obj;
}

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Foo{
    private int id;
    private String name;
    private String city;
    }

任何帮助将不胜感激。

您最初的问题中有一些命名问题,但是无视此问题,您可以根据JSON构造类,以使您自己和Gson都更轻松。

这样的事情会起作用:

public class JsonWrapper {
    public Embedded _embedded;
}


public class Embedded {
    public Customers customers;
}


public class Customers extends ArrayList<Foo>{ }


public class Foo{
    public int id;
    public String name;
    public String city;
}


String json = "{\"_embedded\":{\"customers\":[{\"id\":101,\"name\":\"John\",\"city\":\"Ohio\"},{\"id\":102,\"name\":\"Tom\",\"city\":\"London\"}]}}";
JsonWrapper wrapper = new Gson().fromJson(json, JsonWrapper.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM