Issue
I am trying to create a java model for a JSON structure that looks like this:
"notification": {
"field": "string",
"object": [
{
"field2": "string",
"object2": [
{
"field3": "string",
"object3": {
"code": "string",
"proprietary": "string"
}
}
]
}
]
}
So far my model looks like this:
public class Notification {
private String field = "sample value";
public List<object> sampleObject= new ArrayList<object>();
public String getField() {
return field;
}
public void setField(String notifyingParticipantfinancialInstitutionBIC) {
this.field = field;
}
public List<object> getObject() {
return object;
}
public void setObject(List<object> sampleObject) {
this.sampleObject = sampleObject;
}
}
public class object{
public String field2 = "string";
public List<object2> sampleObject2 = null;
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public List<object2> getObject2() {
return object2;
}
public void setObject2(List<object2> sampleObject2) {
this.object2 = object2;
}
}
I have populated some of the existing fields just to make sure I can print back a json from this POJO model. When I print it using:
ObjectMapper mapper = new ObjectMapper();
Notification test = new Notification();
String jsonInString = mapper.writeValueAsString(test);
i get this JSON object:
"notification": {
"field": "string",
"object": []
}
What am I doing wrong with my 1st object class? I'm not sure why the List is not populating all the other nested variables and objects within it, but returning an empty array.
////
Just fixed it thanks to pleft and Nagaraju Chitimilla's correction! I had to add the new objects to my main object.
Solution
You need to add some objects in your list!
e.g.
Notification test = new Notification();
test.getObject().add(new object()); //Add a new object in your object list
Answered By - pleft
Answer Checked By - Gilberto Lyons (JavaFixing Admin)