gpt4 book ai didi

java - 转换后的 json(来自 ArrayList)有更多元素

转载 作者:行者123 更新时间:2023-12-02 04:12:31 25 4
gpt4 key购买 nike

我发布了我所做的事情,因为我没有得到结果。这里我有一个返回 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()); 其中:

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 类定义:

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

因此,我使用参数字符串类型、字符串描述、字符串产品代码、字符串切割器从构造函数创建一个对象。但是 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 --> 字符串类型,字符串描述,字符串产品代码,字符串切割器

最佳答案

ObjectMapper 默认情况下会序列化类上的所有字段值,无论它们是否为 null,以便您从 Label 类中获取所有内容。

要仅序列化可以配置ObjectMapper的非空值,请参阅 setSerializationInclusion 的 JavaDoc和 Include

mapper.setSerializationInclusion(Include.NON_NULL);

编辑:正如 Maraboc 指出的那样,在使用 Include.NON_NULL 时,quantity 仍然被序列化。为了更精细地控制序列化哪些字段,您可以使用 @JsonIgnore注解以防止类中的其他字段被序列化。

或者您可以将@JsonIgnoreProperties({"quantity"})添加到您的类中

关于java - 转换后的 json(来自 ArrayList)有更多元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com