且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JsonMappingException:当前令牌不是START_OBJECT(需要解包根名'Transaction []'),而是START_ARRAY

更新时间:2022-01-10 17:24:36

您只需要在Transaction顶部添加其他包装类型,也请删除UNWRAP_ROOT_VALUE选项.

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

对于您的示例json,以下内容对我来说很干净:

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);