I am using gson to convert Java objects to Json and I am following the example below.
http://www.studytrails.com/java/json/java-google-json-java-to-json.jsp
What I am not understanding is how to create multiple column and row entries. So I modified the example for my project and the following is my code.
Dataset class for columns:
public class ColsDataset {
private String id;
private String label;
private String pattern;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Bing class
public class Bing {
private ColsDataset[] cols;
private RowsDataset[] rows;
public void setRowsDataset(RowsDataset[] dataset) {
this.rows = dataset;
}
public RowsDataset[] getRowsDataset() {
return rows;
}
public void setColsDataset(ColsDataset[] dataset) {
this.cols = dataset;
}
public ColsDataset[] getColsDataset() {
return cols;
}
}
Main class:
import com.google.gson.Gson;
public class test {
public static void main(String[] args) {
ColsDataset cols1 = new ColsDataset();
cols1.setId("");
cols1.setLabel("Impressions");
cols1.setPattern("");
cols1.setType("number");
ColsDataset cols2 = new ColsDataset();
cols2.setId("");
cols2.setLabel("Spend");
cols2.setPattern("");
cols2.setType("number");
RowsDataset rows = new RowsDataset();
//add row data
Bing bing = new Bing();
bing.setColsDataset(new ColsDataset[] {cols1});
Gson gson = new Gson();
System.out.println(gson.toJson(bing));
}
}
As you can see, I am passing col1 data as the dataset object but not sure how to pass cols2 data as well. I know I am creating a new object for col2 and I probably shouldn't have to do that.
Following is my output right now:
{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"}]}
Desired output:
{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"},{"id":"","label":"Spend","pattern":"","type":"number"}]}
Thanks for your help in advance.