我有這樣的JSON:JSON傑克遜映射到其他構造從我所期待的
{
"label":{
"label":"1D812",
"version":"01"
},
"productionDate":"140415",
"boxNumber":"003",
"quantity":11000,
"order":"0000",
"documentId":"DOC-HHS",
"location":1
}
和我運行以下命令來創建一個Box
對象
ObjectMapper mapper = new ObjectMapper();
Box box = mapper.readValue(myJSON, Box.class);
在Box
類有以下構造函數:
public Box(Label label, String productionDate, String boxNumber, int quantity, String order, String documentId,int location) {
this.label = label;
this.quantity = quantity;
this.order = order;
this.boxNumber = boxNumber;
this.location = location;
this.documentId = documentId;
this.productionDate = productionDate;
}
而在Label
級我有這些構造函數(按順序,如果它的事項)
public Label() {
}
public Label(String label) {
this.label = label;
}
public Label(String label, String version) {
this.label = label;
this.version = version;
}
public Label(String label, String version, SupplierL supplier) {
this.label = label;
this.version = version;
this.supplier = supplier;
this.labelWithVersion = label + "-" + version;
}
當我System.out.println(box.toString());
我看到:
Box{label=Label{label=1D812, version=01, supplier=null}, etc...}
我很納悶,爲什麼它使用的Label
構造與3個參數而不是2?
我剛剛意識到'的System.out.println(box.toString()); '打印'toString()'參數的含義。凡在我的情況下,它'@覆蓋 公共字符串的ToString(){ 回報 「標籤{」 + 「標籤=」 +標籤+ 「版本=」 +版本+ 「供應商=」 +供應商+ '}'; }'..我卡住了一會兒...... – yaylitzis