1

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;
 }
}
asked Feb 28, 2023 at 7:59
1
  • 1
    Consider declaring your internal classes as static. Commented Feb 28, 2023 at 8:22

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! A good option, but I use an immutable class as a dto (I forgot to specify it).
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") }
@wuttke if you find my answer helpful please mark is as accepted.
0

Yes, you can create that directly. Take a look at below code :

  1. Response Class
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 + '}';
 }
 }
}
  1. Main Class
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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.