There is a json with a sheet as a nested class:
{
"name": "test",
"phoneList": {
"phone": [
{
"number": 32323232,
"code": 555
},
{
"number": 4343423432,
"code": 555
}
]
}
}
The following DTO class is suitable for this json:
class Response {
String name;
PhoneList phoneList;
static class PhoneList {
List<Phone> phone = new ArrayList<>();
static class Phone {
String number;
Integer code;
}
}
}
Is it possible somehow not to create a PhoneList class, but to create a sheet directly?
class Response {
String name;
List<Phone> phoneList = new ArrayList<>();
static class Phone {
String number;
Integer code;
}
}
2 Answers 2
You may add below method and it is gonna work like a charm (of course if you make those DTOs proper Jacson pojos (i.e. default constructor + setters/getter)
@JsonProperty("phoneList")
private void mapPhones(Map<String, List<Phone>> phoneList) {
this.phoneList = phoneList.get("phone");
}
answered Feb 28, 2023 at 8:39
Michał Marcinkowski
6204 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
wuttke
Thanks! A good option, but I use an immutable class as a dto (I forgot to specify it).
Michał Marcinkowski
then you may use it in constructor, the syntax exactly the same
public Response (@JsonProperty("name) name, @JsonProperty("phoneList") Map<String, List<Phone>> phoneList) { this.name = name; this.phoneList = phoneList.get("phone") }Michał Marcinkowski
@wuttke if you find my answer helpful please mark is as accepted.
Yes, you can create that directly. Take a look at below code :
ResponseClass
class Response {
String name;
List<Phone> phoneList = new ArrayList<>();
public Response() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Phone> getPhoneList() {
return phoneList;
}
public void setPhoneList(List<Phone> phoneList) {
this.phoneList = phoneList;
}
@Override
public String toString() {
return "Response{" + "name=" + name + ", phoneList=" + phoneList + '}';
}
class Phone {
String number;
Integer code;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Phone() {
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String toString() {
return "Phone{" + "number=" + number + ", code=" + code + '}';
}
}
}
MainClass
public static void main(String[] args) throws JsonProcessingException {
Response response = new Response();
response.setName("test");
Response.Phone phone = response.new Phone();
phone.setNumber("32323232");
phone.setCode(555);
response.phoneList.add(phone);
phone = response.new Phone();
phone.setNumber("4343423432");
phone.setCode(555);
response.phoneList.add(phone);
System.out.println(response.toString());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
System.out.println(mapper.writeValueAsString(response));
}
answered Feb 28, 2023 at 8:35
Jordy
2,0473 gold badges12 silver badges40 bronze badges
Comments
lang-java
static.