![](/img/trans.png)
[英]Java / Jackson - 'Unrecognized token' passing JSON object parameter
[英]Unrecognized token error while converting JSON String to Java Object List
這是JSON
字符串,我想將其轉換為 java object 列表。
String manuallyVerificationRemarks = "[{
"logedInUserName": "forum-admin",
"actionDate": null,
"action": null,
"remarksText": "done by admin..."
}, {
"logedInUserName": null,
"actionDate": null,
"action": null,
"remarksText": null
}]";
下面是我如何將字符串轉換為 object。
private ObjectMapper objectMapper = new ObjectMapper();
try{
remarksDtoList = objectMapper.readValue(manuallyVerificationRemarks, new TypeReference<List<RemarksDTO>>() {});
} catch (Exception e) {
e.printStackTrace();
}
這是目標 DTO
public class RemarksDTO implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
private String logedInUserName;
private Date actionDate;
private String action;
private String remarksText;
RemarksDTO(){
super();
}
public RemarksDTO(String remarks,String userName){
this.remarksText = remarks;
this.logedInUserName=userName;
}
public RemarksDTO(String userName,Date actionDate,String action,String remarks){
this.logedInUserName =userName;
this.action=action;
this.remarksText = remarks;
this.actionDate = actionDate;
}
public String getLogedInUserName() {
return logedInUserName;
}
public void setLogedInUserName(String logedInUserName) {
this.logedInUserName = logedInUserName;
}
public String getRemarksText() {
return remarksText;
}
public void setRemarksText(String remarksText) {
this.remarksText = remarksText;
}
public RemarksDTO(String remarks){
this.remarksText = remarks;
}
public Date getActionDate() {
return actionDate;
}
public void setActionDate(Date actionDate) {
this.actionDate = actionDate;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
@Override
public String toString(){
return "RemarksDTO [logedInUserName=" + logedInUserName + ", remarksText=" + remarksText + "]";
}
}
我收到以下錯誤:匿名類型聲明不能用於評估表達式and here is error trace.... com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'nul': was expecting 'null', 'true', 'false' or NaN at [Source: [{"logedInUserName":"forum-admin","actionDate":nul l,"action":null,"remarksText":"new simple Test is abused done by admin."},{"logedInUserName":null,"actionDate":null,"action":null,"remarksText":null}]; line: 1, column: 51] at [Source: [{"logedInUserName":"forum-admin","actionDate":nul l,"action":null,"remarksText":"new simple Test is abused done by admin."},{"logedInUserName":null,"actionDate":null,"action":null,"remarksText":null}]; line: 1, column: 35] (through reference chain: java.util.ArrayList[0])
and here is error trace.... com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'nul': was expecting 'null', 'true', 'false' or NaN at [Source: [{"logedInUserName":"forum-admin","actionDate":nul l,"action":null,"remarksText":"new simple Test is abused done by admin."},{"logedInUserName":null,"actionDate":null,"action":null,"remarksText":null}]; line: 1, column: 51] at [Source: [{"logedInUserName":"forum-admin","actionDate":nul l,"action":null,"remarksText":"new simple Test is abused done by admin."},{"logedInUserName":null,"actionDate":null,"action":null,"remarksText":null}]; line: 1, column: 35] (through reference chain: java.util.ArrayList[0])
異常消息告訴您輸入中有'nul'而不是null / true / false / NaN值:
[{"logedInUserName":"forum-admin","actionDate":nul
..
[{"logedInUserName":"forum-admin","actionDate":nul
因此,解析器無法將此值映射到DTO中的actionDate屬性。 檢查人工符號上的輸入。
您可以做的一種方法是制作包裝器 class,然后從包裝器一中提取數據並將其放入原始包裝器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.