0

What is the best way to represent below phoneNumbers. I know you can achieve this by defining a list of phoneNumber Class. But I'm not a big fan of creating POJOs, since it's only a single key-value pair. Are there any alternative ways such as Jackson annotation can do the same job? Don't worry about too much on the sample JSON below this is just an example, the phoneNumber array has to be serialized to array of objects

I have below class defined:

class Person {
   private String firstName;
   private String lastName;
   private int[] phoneNumbers; 
}

I expect output something like this:

{
  "firstName": "John",
  "lastName": "Doe",
  "phoneNumbers": [
     { "phoneNumber": "123456" },
     { "phoneNumber": "321123" },
     { "phoneNumber": "123555" }
   ]
}
Chenhai-胡晨海
  • 1,315
  • 9
  • 7
  • 1
    Pojos are great and they are the easiest to work with, but you can probably do something based on https://www.baeldung.com/jackson-custom-serialization i.e. write your own Serializer - another example https://stackoverflow.com/questions/12046786/jackson-json-custom-serialization-for-certain-fields – zapl Sep 21 '22 at 21:22
  • 1
    I would probably just represent each number as a `String`, exactly as supplied in the JSON. Does this program ever manipulate phone numbers as actual numbers? – Tim Moore Sep 21 '22 at 21:56
  • if you want list of phoneNumbers containing an object of String of phoneNumber then you need to define a POJO class for that, and also you class Person is not following the java naming conventions, please refer to this https://www.javatpoint.com/java-naming-conventions for further information. – Aleson Sep 22 '22 at 03:17

0 Answers0