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

ObjectMappernull かどうかに関係なく、デフォルトでクラスのすべてのフィールド値をシリアル化するため、Labelクラスからすべてを取得します。

構成できる null 以外の値のみをシリアル化するには、 setSerializationInclusionおよびIncludeObjectMapperの JavaDoc を参照してください。

mapper.setSerializationInclusion(Include.NON_NULL);

編集: Maraboc が指摘したようにquantityInclude.NON_NULL. どのフィールドをシリアル化するかをより細かく制御するには、@JsonIgnore注釈を使用して、クラス内の他のフィールドがシリアル化されないようにすることができます。

@JsonIgnoreProperties({"quantity"})または、クラスに追加できます

于 2015-11-12T10:38:27.290 に答える
0

JsonSerialize アノテーションを使用して Label クラスを定義し、数量のタイプをプリミティブ int から Integer Object に変更できます。type が 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 に答える