简体   繁体   English

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

[英]Unrecognized token error while converting JSON String to Java Object List

here is the JSON string which I want to convert as 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
 }]";

And here is how I am converting string to object.下面是我如何将字符串转换为 object。

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

And here is Target DTO这是目标 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 + "]";

    }
}

I am getting following errors: anonymous type declaration cannot be used in an evaluation expression 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]) 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])

Exception message tells you have 'nul' instead of null/true/false/NaN values in your input: 异常消息告诉您输入中有'nul'而不是null / true / false / NaN值:

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

So, pareser just can't map this value to actionDate property in your DTO. 因此,解析器无法将此值映射到DTO中的actionDate属性。 Check input on artificial symbols. 检查人工符号上的输入。

The One way you can do, is to make the wrapper class and then extract data from wrapper one and put it in the original one.您可以做的一种方法是制作包装器 class,然后从包装器一中提取数据并将其放入原始包装器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM