转换的json(来自ArrayList)具有更多元素

时间:2015-11-12 10:31:48

标签: java json to-json

我发布了我所做的事情,因为我没有得到结果..这里我有一个返回ArrayList的方法:

#app/views/teams/new.html.erb
<%= form_for @team do |f| %>
   <%= f.collection_select :speakers, @usernames, :id, :name, multiple: true %>
   <%= f.submit %>
<% end %>

然后我希望将此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; } 格式。所以我执行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

我不明白为什么json文件有这么多属性?我的对象应该只有4 - &gt; [{ "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

2 个答案:

答案 0 :(得分:2)

ObjectMapper will by default serialise all field values on a class, regardless of whether they are null or not so you get everything from your Label class.

To only serialise non null values that you can configure the ObjectMapper see the JavaDoc for setSerializationInclusion and Include

mapper.setSerializationInclusion(Include.NON_NULL);

EDIT: As Maraboc pointed out you have the problem with quantity still being serialised when using the Include.NON_NULL. For finer grain control of which fields are serialised you can use @JsonIgnore annotation to prevent the other fields in your class from being serialised.

Or you can add @JsonIgnoreProperties({"quantity"}) to your class

答案 1 :(得分:0)

You can define your Label class with JsonSerialize annotation and change the type of quantity from primitive int to Integer Object. If type is int, the default value zero will be assigned to the variable.

@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;
    }
}