JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY

后端 未结 1 1797
梦谈多话
梦谈多话 2021-01-07 02:10

I\'m using Spring\'s RestTemplate to consume the Chargify API, using JSON as the payload format. It has been going pretty smoothly, however when I try to GET an array of obj

1条回答
  •  再見小時候
    2021-01-07 02:56

    You just need another wrapper type on top of Transaction, also please remove the UNWRAP_ROOT_VALUE option.

    The classes will be along these lines:

    public class TransactionHolder {
        private Transaction transaction;
    ... 
    }
    
    public class Transaction {
        private String amount_in_cents;
        private String created_at;
        private int ending_balance_in_cents;
        private int id;
        private String kind;
        private String memo;
        private int payment_id;
        private int product_id;
        private int subscription_id;
        private boolean success;
        private String transaction_type;
        private String type;
    ...
    }
    

    With your sample json, the following works for me cleanly:

    ObjectMapper mapper = new ObjectMapper();
    InputStream is = this.getClass().getResourceAsStream("sample.json");
    TransactionHolder[] holders = mapper.readValue(is,TransactionHolder[].class); 
    

    0 讨论(0)
提交回复
热议问题