Converted json (from ArrayList) 有更多的元素
Converted json (from ArrayList) has more elements
我 post 我做了什么,因为我没有得到结果..这里我有一个方法 returns 一个 ArrayList:
public ArrayList<Label> getLabels()
throws ClassNotFoundException, SQLException{
ArrayList<Label> labels = new ArrayList<>();
sq = "SELECT * from LABELS";
try {
Class.forName(typeDB);
c = DriverManager.getConnection(path);
stm = c.prepareStatement(sq);
ResultSet rs = stm.executeQuery();
while(rs.next()) {
Label label = new Label(rs.getString("type"), rs.getString("description"),rs.getString("product")+"-"+rs.getString("version"), rs.getString("cutter"));
labels.add(label);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (stm != null)
stm.close();
if (c != null)
c.close();
}
System.out.println("Label "+ labels.size());
return labels;
}
然后我想将这个 ArrayList 转换为 JSON
格式。所以我执行 labelsToJSON(action.getLabels());
where:
public void labelsToJSON(ArrayList<Label> list){
ObjectMapper mapper = new ObjectMapper();
try{
mapper.writeValue(new File("C:\temp\labels.json"), list);
}catch(JsonGenerationException e){
e.printStackTrace();
}catch(JsonMappingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
Label
class的定义是:
public class Label {
private String barcode;
private String labelCode;
private String productCode;
private String type;
//and many others..
public Label(){
}
//This is the costructor I use above in the method
public Label(String type, String description, String productCode, String cutter) {
this.type = type;
this.description = description;
this.productCode = productCode;
this.cutter = cutter;
}
//and then some other constructors (I post 2 for example)
public Label(String type, String description, String product, String version, String cutter) {
this.type = type;
this.description = description;
this.product = product;
this.version = version;
this.cutter = cutter;
}
public Label(String barcode, String product, String version, String dateProduction, String order , int quantity, String packetNumber, String type, String description, String cutter) {
this.barcode = barcode;
this.product = product;
this.version = version;
this.dateProduction = dateProduction;
this.order = order;
this.packetNumber = packetNumber;
this.quantity = quantity;
this.type = type;
this.description = description;
this.cutter = cutter;
}
//setters, getters etc
因此,我使用参数 String type, String description, String productCode, String cutter
从构造函数创建了一个对象。但是 labels.json
包含这些数据
[{
"barcode":null,
"labelCode":null,
"productCode":"111123123-1123", //<-
"type":"Container", //<-
"description":"this is a description", //<- all these I was expected.
"cutter":"1031", //<-
"date":null,
"time":null,
"dateProduction":null,
"order":null,
"product":null,
"version":null,
"packetNumber":null,
"quantity":0
}, //and so on
我不明白为什么json文件有这么多属性??我的对象应该只有 4 --> String type, String description, String productCode, String cutter
ObjectMapper
将默认序列化 class 上的所有字段值,无论它们是否为空,因此您可以从 Label
class 中获取所有内容。
要仅序列化您可以配置 ObjectMapper
的非空值,请参阅 setSerializationInclusion and Include
的 JavaDoc
mapper.setSerializationInclusion(Include.NON_NULL);
编辑:
正如 Maraboc 指出的那样,您遇到了在使用 Include.NON_NULL
时 quantity
仍然被序列化的问题。为了更好地控制哪些字段被序列化,您可以使用 @JsonIgnore
注释来防止 class 中的其他字段被序列化。
或者您可以将 @JsonIgnoreProperties({"quantity"})
添加到您的 class
您可以使用 JsonSerialize 注释定义 Label class 并将数量类型从原始 int 更改为 Integer Object。如果类型为 int,则默认值零将分配给变量。
@JsonSerialize(
include=JsonSerialize.Inclusion.NON_NULL)
public class Label {
// ...other properties
private Integer quantity;
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
我 post 我做了什么,因为我没有得到结果..这里我有一个方法 returns 一个 ArrayList:
public ArrayList<Label> getLabels()
throws ClassNotFoundException, SQLException{
ArrayList<Label> labels = new ArrayList<>();
sq = "SELECT * from LABELS";
try {
Class.forName(typeDB);
c = DriverManager.getConnection(path);
stm = c.prepareStatement(sq);
ResultSet rs = stm.executeQuery();
while(rs.next()) {
Label label = new Label(rs.getString("type"), rs.getString("description"),rs.getString("product")+"-"+rs.getString("version"), rs.getString("cutter"));
labels.add(label);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (stm != null)
stm.close();
if (c != null)
c.close();
}
System.out.println("Label "+ labels.size());
return labels;
}
然后我想将这个 ArrayList 转换为 JSON
格式。所以我执行 labelsToJSON(action.getLabels());
where:
public void labelsToJSON(ArrayList<Label> list){
ObjectMapper mapper = new ObjectMapper();
try{
mapper.writeValue(new File("C:\temp\labels.json"), list);
}catch(JsonGenerationException e){
e.printStackTrace();
}catch(JsonMappingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
Label
class的定义是:
public class Label {
private String barcode;
private String labelCode;
private String productCode;
private String type;
//and many others..
public Label(){
}
//This is the costructor I use above in the method
public Label(String type, String description, String productCode, String cutter) {
this.type = type;
this.description = description;
this.productCode = productCode;
this.cutter = cutter;
}
//and then some other constructors (I post 2 for example)
public Label(String type, String description, String product, String version, String cutter) {
this.type = type;
this.description = description;
this.product = product;
this.version = version;
this.cutter = cutter;
}
public Label(String barcode, String product, String version, String dateProduction, String order , int quantity, String packetNumber, String type, String description, String cutter) {
this.barcode = barcode;
this.product = product;
this.version = version;
this.dateProduction = dateProduction;
this.order = order;
this.packetNumber = packetNumber;
this.quantity = quantity;
this.type = type;
this.description = description;
this.cutter = cutter;
}
//setters, getters etc
因此,我使用参数 String type, String description, String productCode, String cutter
从构造函数创建了一个对象。但是 labels.json
包含这些数据
[{
"barcode":null,
"labelCode":null,
"productCode":"111123123-1123", //<-
"type":"Container", //<-
"description":"this is a description", //<- all these I was expected.
"cutter":"1031", //<-
"date":null,
"time":null,
"dateProduction":null,
"order":null,
"product":null,
"version":null,
"packetNumber":null,
"quantity":0
}, //and so on
我不明白为什么json文件有这么多属性??我的对象应该只有 4 --> String type, String description, String productCode, String cutter
ObjectMapper
将默认序列化 class 上的所有字段值,无论它们是否为空,因此您可以从 Label
class 中获取所有内容。
要仅序列化您可以配置 ObjectMapper
的非空值,请参阅 setSerializationInclusion and Include
mapper.setSerializationInclusion(Include.NON_NULL);
编辑:
正如 Maraboc 指出的那样,您遇到了在使用 Include.NON_NULL
时 quantity
仍然被序列化的问题。为了更好地控制哪些字段被序列化,您可以使用 @JsonIgnore
注释来防止 class 中的其他字段被序列化。
或者您可以将 @JsonIgnoreProperties({"quantity"})
添加到您的 class
您可以使用 JsonSerialize 注释定义 Label class 并将数量类型从原始 int 更改为 Integer Object。如果类型为 int,则默认值零将分配给变量。
@JsonSerialize(
include=JsonSerialize.Inclusion.NON_NULL)
public class Label {
// ...other properties
private Integer quantity;
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}