Reputation: 1205
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..
Upvotes: 0
Views: 412
Reputation: 307
The structure of your json does not follow the pattern, since results, so you left it look like, should be an array.
See that https://www.json.org/.
What could be done would be the following structure:
{ results: [ {"id": 1, "text": "Option 1"}, {"id": 2, "text": "Option 2"} ] }
You need to put in your array a value, however you are always updating your references to a new array:
...
private Integer[] id = new Integer [100];
...
public void setId(Integer[] id){
// here you are adding the reference to a new array, overwriting your id reference to which you started with a new Integer[100]
this.id = id;
}
In order for the array to store the ids you may need a new property to store the current position. And your setId should get a reference to an Integer instead of an Integers Array, and add to this array the value.
private Integer[] id = new Integer[100];
private int nextPosition = 0;
...
// I omit the treatment here so that it does not add a position beyond what its array allows for simplification purposes.
public void setId(Integer[] id) {
this.id[nextPosition] = id;
nextPosition = nextPosition + 1;
}
...
So you would persist more values, but from what I understood is not exactly what you need, since the structure of the outcome would continue to be the same for which you are complaining:
// No association between ids and texts
{
"results":
{
"id": [3509, 8987, 1234, 777, 987],
"text": ["text 1", "text 2", "text 3"]
}
}
Note that results is an array of a complex object, not a Value Object such as String and Integer. Perhaps the ideal would be you to create a new object to behave and associate the id and text properties.
public class ResultsOrganisationChild {
private Integer id;
private String text;
public ResultsOrganisationChild(Integer id, String text){
this.id = id;
this.text = text;
}
// add some gets and sets
}
Optionally, you could use a List instead of an array, because then you will not have to worry about your object growing beyond what you might have initially foreseen, besides not having to worry about the next position:
public class ResultsOrganisationUnit {
private List<ResultsOrganisationChild> list = new ArrayList<>();
public void addChild(ResultsOrganisationChild child) {
list.add(child);
}
public List<ResultsOrganisationChild> getChild() {
return this.list;
}
}
Your for-each would look like this:
ResultsOrganisationUnit results = new ResultsOrganisationUnit();
for(OrganizationUnitNameHistory children: children2) {
ResultsOrganisationChild child = new ResultsOrganisationChild (children.getOrganisationunitCode(), children.getName());
results.addChild(child);
}
Upvotes: 0
Reputation: 3019
You are getting the last value, because that is what you ask your code to do.
I am not sure what kind of variabel the children2 is, but lets say its a List<ResultsOrganisationUnit>
then one solution would be to:
private Integer[] ids = new Integer[100];
private String[] texts = new String[100];
for ( int i=0; i < children2.size(); i++) {
ids[i] = children2[i].getOrganisationunitCode();
texts[i] = children2[i].getName();
}
results.setId(ids);
results.setText(texts);
But still the task as you are referring to is not accomplished by your code. What you will be looking at is something similar to the following, depending on the values you provide:
{
"results":
{
"id":[1, 2, 3],
"text":["text 1", "text 2", "text 3"]
}
}
To be able to get what you are asking for in the comment is to create your OrganisationUnit
like this:
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;}
}
And then store the values for the results in a List<OrganisationUnit>
List<OrganisationUnit> results = new ArrayList();
for (OrganisationUnitNameHistory child : children2) {
OrganisationUnit tempChild = new OrganisationUnit();
tempChild.setId(child.getOrganisationunitCode());
tempChild.setText(child.getName());
results.add(tempChild);
}
The results that you return should be according what you are looking for in the comment field.
Upvotes: 0
Reputation: 106
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);
}
Upvotes: 1