【问题标题】:Java to Json using Gson使用 Gson 的 Java 到 Json
【发布时间】:2014-10-29 21:26:01
【问题描述】:

我正在使用 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"}]}

提前感谢您的帮助。

【问题讨论】:

  • new ColsDataset[] {cols1, cols2} ?
  • 使用List<ColsDataset> 而不是ColsDataset[]... Un/Marshalling 不适用于原始数组类型。

标签: java json gson


【解决方案1】:

将您的对象数组转换为列表。这应该适用于将数据编组为 JSON 并将其解组回 Java 字节码。

看看这个教程:"How To Convert Java Object To / From JSON (Gson)" from Mkyong.com

必应

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的更多信息,请访问:Stack Overflow: Should JSON include null values

【讨论】:

  • 我是否必须为行传递嵌套列表。我正在尝试获取行的以下结果。 "行": [ {"c":[{"v":"蘑菇","f":null},{"v":3,"f":null}]}, {"c":[{ "v":"洋葱","f":null},{"v":1,"f":null}]}, {"c":[{"v":"橄榄","f": null},{"v":1,"f":null}]}, {"c":[{"v":"Zucchini","f":null},{"v":1,"f ":null}]}, {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]} ]
  • rows 应该是行数据列表。 vf 是什么?
  • V 和 f 是我将在创建 RowDataset 对象时传递的值。 “行”中的每个值都是一个列表,每个列表都有一个列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多