Search code examples
javajsonfasterxml

How to deserialize arrays via fasterxml?


I have the following 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() {
        }
    }

}

and here is the json i'm getting

{
    "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
        }
    ]
}

but i'm getting an exception

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"])

I've annotated the member as an array type for the json deserilize as you can see, but still no result.

How should I do this?

Thanks in advance


Solution

  • Try make inner class ststic. Jackson needs to access it like: new GetReceiptResponse.SubTotal()