将JSON字符串转换为Java对象列表时无法识别的令牌错误

时间:2017-03-25 07:52:31

标签: java json list

这里是我想要转换为java对象List的JSON字符串。

String manuallyVerificationRemarks = "[{
    "logedInUserName": "forum-admin",
    "actionDate": null,
    "action": null,
    "remarksText": "done by admin..."
 }, {
    "logedInUserName": null,
    "actionDate": null,
    "action": null,
    "remarksText": null
 }]";

以下是我将字符串转换为对象的方式。

private ObjectMapper objectMapper = new ObjectMapper();
try{
    remarksDtoList = objectMapper.readValue(manuallyVerificationRemarks, new TypeReference<List<RemarksDTO>>() {});
} catch (Exception e) {
    e.printStackTrace();
}

这是Target 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])

1 个答案:

答案 0 :(得分:0)

异常消息告诉您输入中有'nul'而不是null / true / false / NaN值:

[{"logedInUserName":"forum-admin","actionDate":nul
..
[{"logedInUserName":"forum-admin","actionDate":nul

因此,pareser无法将此值映射到DTO中的actionDate属性。检查人工符号的输入。