I have below json
{
"startDate": "2021-02-01",
"endDate": "2021-02-14",
"columns": [
{
"attribute": "PID"
},
{
"attribute": "CID"
},
{
"attribute": "SID"
}
],
"ids": [
{
"id": "123456A",
"idType": "PID"
}
]
}
As you can see columns array have same key 'attribute'. I created pojo for this but i am not able to add data.
-----------POJO--------
public class Columns {
private String attribute;
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
}
Other pojo
public List<Columns> getColumns() {
return columns;
}
public void setColumns(List<Columns> columns) {
this.columns = columns;
}
I am adding data like this
Columns c=new Columns();
c.setAttribute("PID");
List<Columns> l=new ArrayList<Columns>();
l.add(c);
c.setAttribute("CID");
l.add(c);
c.setAttribute("SID");
l.add(c);
m.setColumns(l);
and its giving output like this (value of m)
"columns": [
{
"attribute": "SID"
},
{
"attribute": "SID"
},
{
"attribute": "SID"
}
],
What i am doing wrong ?
I think you should create a class Column
(singular) so you can create a list like so:
List<Column> l=new ArrayList<Column>();
c = new Column();
c.setAttribute("PID");
l.add(c);
c2 = new Column();
c.setAttribute("SID");
l.add(c2);
...
You likely want to use a loop above.
In your current setup, you keep changing the c
object which means that the last change you make to it will be assigned to all items (they all point to c
).