无法将 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);