I have a List<Person> list.
I want to serialize it to JSON with some other attributes, using Jackson Api.
The output should be like this:
{
"sEcho": 3,
"iTotalRecords": 10,
"iTotalDisplayRecords": 10,
"aaData": [ <--here's the persons
{
"name": "TestName1",
"status": "ok"
},
{
"name": "TestName2",
"status": "ok"
},
...
]
}
Probably very simple but couldn't figure it from Jackson's Api. Thanks
I'd create a new class called PersonGroup having any of the extra fields you need with the List as another field on this class - for the example you gave this field would be named aaData.
This would represent the structure you have here. If you think a new class is too much then you could just make a HashMap of Objects and store the extra fields as whatever object you like and then add these to the HashMap, making sure the keys match the name of the extra fields and making sure your List is also in the HashMap.
Deserializing this class or HashMap should return the output you mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With