0

我发布了我所做的,因为我没有得到结果。这里我有一个返回 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

所以,我从带有参数的构造函数创建一个对象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

4

2 回答 2

2

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

要仅序列化您可以配置的非空值,ObjectMapper请参阅 JavaDoc for setSerializationInclusionInclude

mapper.setSerializationInclusion(Include.NON_NULL);

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

或者你可以添加@JsonIgnoreProperties({"quantity"})到你的班级

于 2015-11-12T10:38:27.290 回答
0

您可以使用 JsonSerialize 注释定义您的 Label 类,并将数量的类型从原始 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;
    }
}   
于 2015-11-12T10:47:12.660 回答