如何通过 fasterxml 反序列化数组?
How to deserialize arrays via fasterxml?
我有以下 class
@JsonIgnoreProperties({"error"})
public class GetReceiptResponse {
//Cashier Id
public String cid;
//Created
public String time;
//TotalAmount
public String ta;
//Cash amount
public String cash;
//Card amount
public String card;
//PartialPayment amount
public String ppa;
//PrePaymentUsage amount
public String ppu;
//Lottery number
public String lot;
//OperationType(0 = ORDER, 2 = RETURN, 3 = PREPAYMENT)
public String type;
//Referenced Receipt Id
public String ref;
//Referenced crn
public String refcrn;
//Products, or Departments
@JsonDeserialize(as = SubTotal[].class)
public SubTotal[] totals;
public GetReceiptResponse() {
}
public class SubTotal {
public String did; //Department Id
public String dt; //Department Tax
public String dtm; //Department Tax Mode
public String t; //Total
public String tt; //Total With Tax
public String id; //Product Id
public String gc; //Good Code
public String gn; //Good Name
public String qty; //Quantity
public String p; //Price
public String adg; //ATG Code
public String mu; //Measurement Unit
public String dsc; //Discount
public String adsc; //AdditionalDiscount
public String dsct; //DiscountType
public String rpid; //ReceiptProductId
public SubTotal() {
}
}
}
这是我得到的json
{
"time": 1453881919000,
"lot": "28543833",
"type": 0,
"cid": 3,
"ta": 6000,
"cash": 6000,
"card": 0,
"ppa": 0,
"ppu": 0,
"saleType": 2,
"totals": [
{
"gc": "002",
"gn": "Fanta",
"qty": 3,
"p": 1000,
"mu": "տուփ",
"rpid": 1,
"did": 1,
"dt": 16.67,
"dtm": 1,
"t": 2499.9,
"tt": 3000
},
{
"gc": "001",
"gn": "Coca Cola",
"qty": 3,
"p": 1000,
"mu": "տուփ",
"rpid": 0,
"did": 1,
"dt": 16.67,
"dtm": 1,
"t": 2499.9,
"tt": 3000
}
]
}
但我遇到了一个例外
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class am.iunetworks.ecr.client.V04.pojo.GetReceiptResponse$SubTotal]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: [B@44e3a2b2; line: 1, column: 169] (through reference chain: am.iunetworks.ecr.client.V04.pojo.GetReceiptResponse["totals"])
如您所见,我已将成员注释为 json 反序列化的数组类型,但仍然没有结果。
我应该怎么做?
提前致谢
尝试使内部 class 静态化。杰克逊需要像这样访问它:new GetReceiptResponse.SubTotal()
我有以下 class
@JsonIgnoreProperties({"error"})
public class GetReceiptResponse {
//Cashier Id
public String cid;
//Created
public String time;
//TotalAmount
public String ta;
//Cash amount
public String cash;
//Card amount
public String card;
//PartialPayment amount
public String ppa;
//PrePaymentUsage amount
public String ppu;
//Lottery number
public String lot;
//OperationType(0 = ORDER, 2 = RETURN, 3 = PREPAYMENT)
public String type;
//Referenced Receipt Id
public String ref;
//Referenced crn
public String refcrn;
//Products, or Departments
@JsonDeserialize(as = SubTotal[].class)
public SubTotal[] totals;
public GetReceiptResponse() {
}
public class SubTotal {
public String did; //Department Id
public String dt; //Department Tax
public String dtm; //Department Tax Mode
public String t; //Total
public String tt; //Total With Tax
public String id; //Product Id
public String gc; //Good Code
public String gn; //Good Name
public String qty; //Quantity
public String p; //Price
public String adg; //ATG Code
public String mu; //Measurement Unit
public String dsc; //Discount
public String adsc; //AdditionalDiscount
public String dsct; //DiscountType
public String rpid; //ReceiptProductId
public SubTotal() {
}
}
}
这是我得到的json
{
"time": 1453881919000,
"lot": "28543833",
"type": 0,
"cid": 3,
"ta": 6000,
"cash": 6000,
"card": 0,
"ppa": 0,
"ppu": 0,
"saleType": 2,
"totals": [
{
"gc": "002",
"gn": "Fanta",
"qty": 3,
"p": 1000,
"mu": "տուփ",
"rpid": 1,
"did": 1,
"dt": 16.67,
"dtm": 1,
"t": 2499.9,
"tt": 3000
},
{
"gc": "001",
"gn": "Coca Cola",
"qty": 3,
"p": 1000,
"mu": "տուփ",
"rpid": 0,
"did": 1,
"dt": 16.67,
"dtm": 1,
"t": 2499.9,
"tt": 3000
}
]
}
但我遇到了一个例外
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class am.iunetworks.ecr.client.V04.pojo.GetReceiptResponse$SubTotal]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: [B@44e3a2b2; line: 1, column: 169] (through reference chain: am.iunetworks.ecr.client.V04.pojo.GetReceiptResponse["totals"])
如您所见,我已将成员注释为 json 反序列化的数组类型,但仍然没有结果。
我应该怎么做?
提前致谢
尝试使内部 class 静态化。杰克逊需要像这样访问它:new GetReceiptResponse.SubTotal()