如何用杰克逊解组对象
How to unmarshall object with jackson
鉴于JSON如下
[{"itemId":6,"itemTypeId":2,"expDate":"2021-04-17T22:00:00.000+00:00","creationDate":"2021-04-18T09:44:52.417+00:00","transactions":[{"transactionType":"USE","userId":0,"quantityBefore":6.0,"quantityAfter":4.0,"locIdBefore":2,"locIdAfter":2}]}]
我正在尝试将 JSON 解组为如上定义的 POJO,但是得到
UnsupportedOperationException
我的想法是使用简单的杰克逊映射:
public static ArrayList<HistoryItem> convert (String response){
ObjectMapper mapper = new ObjectMapper();
ArrayList<HistoryItem> itemList = new ArrayList<>();
try {
itemList = (ArrayList<HistoryItem>)mapper.readValue(response, new TypeReference<List<HistoryItem>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return itemList;
}
对于具有简单对象类型(如字符串、长整型、整数等)字段的简单对象,它工作正常,但是当我添加 ArrayList 时,我得到了那个错误。知道我的问题是什么吗?
我的 class HistoryItemDto:
public class HistoryItemDto {
private Long itemId;
private Long itemTypeId;
private Date expDate;
private Date creationDate;
private ArrayList<HistoryTransactionDto> transactions;}
我的 class HistoryTransactionDto:
public class HistoryTransactionDto {
private TransactionType transactionType;
private Long userId;
private float quantityBefore;
private float quantityAfter;
private Long locIdBefore;
private Long locIdAfter;}
它们都包括 getters、setters、无参数构造函数和每个参数构造函数。
提前....检查了很多网站,如 baeldung、jackson 文档、Whosebug 的帖子,但没有找到任何答案回答我的原因。
我遇到了同样的问题,为了解决这个问题,不使用列表(包括 ArrayList 或任何其他类型的迭代器),而是使用基本数组:
public static ArrayList<HistoryItem> convert (String response) {
ObjectMapper mapper = new ObjectMapper();
ArrayList<HistoryItem> itemAsArrayList = new ArrayList<>();
try {
HistoryItem[] responseAsArray = mapper.readValue(response, HistoryItem[].class);
Collections.addAll(itemAsArrayList, responseAsArray);
} catch (IOException e) {
e.printStackTrace();
}
return itemAsArrayList;
}
鉴于JSON如下
[{"itemId":6,"itemTypeId":2,"expDate":"2021-04-17T22:00:00.000+00:00","creationDate":"2021-04-18T09:44:52.417+00:00","transactions":[{"transactionType":"USE","userId":0,"quantityBefore":6.0,"quantityAfter":4.0,"locIdBefore":2,"locIdAfter":2}]}]
我正在尝试将 JSON 解组为如上定义的 POJO,但是得到
UnsupportedOperationException
我的想法是使用简单的杰克逊映射:
public static ArrayList<HistoryItem> convert (String response){
ObjectMapper mapper = new ObjectMapper();
ArrayList<HistoryItem> itemList = new ArrayList<>();
try {
itemList = (ArrayList<HistoryItem>)mapper.readValue(response, new TypeReference<List<HistoryItem>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return itemList;
}
对于具有简单对象类型(如字符串、长整型、整数等)字段的简单对象,它工作正常,但是当我添加 ArrayList 时,我得到了那个错误。知道我的问题是什么吗?
我的 class HistoryItemDto:
public class HistoryItemDto {
private Long itemId;
private Long itemTypeId;
private Date expDate;
private Date creationDate;
private ArrayList<HistoryTransactionDto> transactions;}
我的 class HistoryTransactionDto:
public class HistoryTransactionDto {
private TransactionType transactionType;
private Long userId;
private float quantityBefore;
private float quantityAfter;
private Long locIdBefore;
private Long locIdAfter;}
它们都包括 getters、setters、无参数构造函数和每个参数构造函数。
提前....检查了很多网站,如 baeldung、jackson 文档、Whosebug 的帖子,但没有找到任何答案回答我的原因。
我遇到了同样的问题,为了解决这个问题,不使用列表(包括 ArrayList 或任何其他类型的迭代器),而是使用基本数组:
public static ArrayList<HistoryItem> convert (String response) {
ObjectMapper mapper = new ObjectMapper();
ArrayList<HistoryItem> itemAsArrayList = new ArrayList<>();
try {
HistoryItem[] responseAsArray = mapper.readValue(response, HistoryItem[].class);
Collections.addAll(itemAsArrayList, responseAsArray);
} catch (IOException e) {
e.printStackTrace();
}
return itemAsArrayList;
}