Search code examples
javaarraysjsonstruts

How to fill out array properties of the object with multiple values


I do use strust-json library for converting java object to json automatically. The task is to provide the following output from the server to client:

{
"results": 
{
  "id": 1,
  "text": "Option 1"
},
{
  "id": 2,
  "text": "Option 2"
}
} 

I've created object with array properties:

public class ResultsOrganisationUnit {

private Integer[] id = new Integer[100];
private String[] text = new String[100];
public Integer[] getId() {
    return id;
}
public void setId(Integer[] id) {
    this.id = id;
}
public String[] getText() {
    return text;
}
public void setText(String[] text) {
    this.text = text;
}           

}

Initialized it:

results = new ResultsOrganisationUnit();

and then later I tried to fill it out using for-each cycle:

for (OrganisationUnitNameHistory children : children2){
            results.setId(new Integer[] {children.getOrganisationunitCode()});
            results.setText(new String[] {children.getName()});

        }

The thing is, I only get the last value of array in the output from client side.

{"results":{"id":[3509],"text":["text 1"]}}

However, the object children for example has all 5 values. It seems like I'm overriding the same array value each time, instead of filling out the new one. I'm confused where is that logical error, which I'm making. It must be something simple..


Solution

  • You should wrap id and text into new class, and store collection of them inside result:

    public class OrganisationUnit {
        private Integer id;
        private String text;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
    }
    
    public class Results {
    
        private List<OrganisationUnit> results = new ArrayList<>();
    
        public List<OrganisationUnit> getResults() {
            return results;
        }
    
        public void setResults(List<OrganisationUnit> results) {
            this.results = results;
        }
    }
    

    Fill it using loop:

        Results results = new Results();
    
        for (OrganisationUnitNameHistory children : children2) {
            OrganisationUnit ou = new OrganisationUnit();
            ou.setId(children.getOrganisationunitCode());
            ou.setText(children.getName());
            results.getResults().add(ou);
        }
    

    Or directly without using Result class:

    List<OrganisationUnit> results = new ArrayList<>();
    
    for (OrganisationUnitNameHistory children : children2) {
            OrganisationUnit ou = new OrganisationUnit();
            ou.setId(children.getOrganisationunitCode());
            ou.setText(children.getName());
            results.add(ou);
    }