[英]Java to Json using Gson
我正在使用gson将Java对象转换为Json,并遵循以下示例。
http://www.studytrails.com/java/json/java-google-json-java-to-json.jsp
我不了解的是如何创建多个列和行条目。 因此,我为项目修改了示例,以下是我的代码。
列的数据集类:
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;
}
}
兵课
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;
}
}
主班:
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));
}
}
如您所见,我将col1数据作为数据集对象传递,但是不确定如何传递cols2数据。 我知道我正在为col2创建一个新对象,我可能不必这样做。
以下是我现在的输出:
{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"}]}
所需的输出:
{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"},{"id":"","label":"Spend","pattern":"","type":"number"}]}
感谢您的帮助。
将您的对象数组转换为列表。 这应该适用于将数据编组到JSON并将其编组回Java字节码。
看一下本教程:来自Mkyong.com的“如何将Java对象转换为JSON(/ JSON)(Gson)”
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;
}
}
将第二个ColsDataset
对象添加到同一列表有什么问题?
bing.setColsDataset(Arrays.asList(cols1, cols2));
输出是您期望的:
{"cols":[{"id":"","label":"Impressions","pattern":"","type":"number"},{"id":"","label":"Spend","pattern":"","type":"number"}]}
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));
}
}
关于下面的问题。
{
"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
}]
}]
}
null
值将不会显示在结果JSON中,但可以读取。
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;
}
}
}
有关null
更多信息,请访问: 堆栈溢出:JSON是否应包含null值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.