Sohail
Sohail

Reputation: 1147

Java to Json using Gson

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.

Upvotes: 1

Views: 1071

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48723

Convert your Object arrays to Lists. This should work for marshalling data to JSON and unmarshalling it back into Java Bytecode.

Take a look at this tutorial: "How To Convert Java Object To / From JSON (Gson)" from Mkyong.com

Bing

import java.util.List;

public class Bing {
    private List<ColsDataset> cols;
    private List<RowsDataset> rows;

    public Bing() { }

    public void setColsDataset(List<ColsDataset> dataset) {
        this.cols = dataset;
    }

    public List<ColsDataset> getColsDataset() {
        return cols;
    }

    public void setRowsDataset(List<RowsDataset> dataset) {
        this.rows = dataset;
    }

    public List<RowsDataset> getRowsDataset() {
        return rows;
    }
}

What's wrong with adding the second ColsDataset object to the same list?

bing.setColsDataset(Arrays.asList(cols1, cols2));

The output is what you expected:

{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"},{"id":"","label":"Spend","pattern":"","type":"number"}]}

Test

import java.util.Arrays;

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();

        //TODO Add row data.

        Bing bing = new Bing();
        bing.setColsDataset(Arrays.asList(cols1, cols2));

        Gson gson = new Gson();

        System.out.println(gson.toJson(bing));
    }
}

Extension

In regards to your question below.

{
    "rows": [{
        "c": [{
            "v": "Mushrooms"
        }, {
            "v": 1
        }]
    }, {
        "c": [{
            "v": "Onions"
        }, {
            "v": 1
        }]
    }, {
        "c": [{
            "v": "Olives"
        }, {
            "v": 1
        }]
    }, {
        "c": [{
            "v": "Zucchini"
        }, {
            "v": 1
        }]
    }, {
        "c": [{
            "v": "Pepperoni"
        }, {
            "v": 1
        }]
    }]
}

The null values will not show in the resulting JSON, but can be read in.

import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;

public class Test {
    public static void main(String[] args) {
        Table table = createTable(
            createRow(
                createCol("Mushrooms", null),
                createCol(1, null)),
            createRow(
                createCol("Onions", null),
                createCol(1, null)),
            createRow(
                createCol("Olives", null),
                createCol(1, null)),
            createRow(
                createCol("Zucchini", null),
                createCol(1, null)),
            createRow(
                createCol("Pepperoni", null),
                createCol(1, null)));

        System.out.println(new Gson().toJson(table));
    }

    public static Table createTable(Row... rows) {
        Table table = new Table();
        table.setRows(Arrays.asList(rows));
        return table;
    }

    public static Row createRow(Col... cols) {
        Row row = new Row();
        row.setC(Arrays.asList(cols));
        return row;
    }

    public static Col createCol(Object v, Object f) {
        Col col = new Col();
        col.setV(v);
        col.setF(f);
        return col;
    }

    static class Table {
        private List<Row> rows;

        public Table() { }

        public List<Row> getRows() {
            return rows;
        }

        public void setRows(List<Row> rows) {
            this.rows = rows;
        }
    }

    static class Row {
        private List<Col> c;

        public Row() { }

        public List<Col> getC() {
            return c;
        }

        public void setC(List<Col> c) {
            this.c = c;
        }
    }

    static class Col {
        private Object v;
        private Object f;

        public Col() { }

        public Object getV() {
            return v;
        }

        public void setV(Object v) {
            this.v = v;
        }

        public Object getF() {
            return f;
        }

        public void setF(Object f) {
            this.f = f;
        }
    }
}

For more on null, visit: Stack Overflow: Should JSON include null values

Upvotes: 1

Related Questions