简体   繁体   中英

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.

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.

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

And here is 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 + "]";

    }
}

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

Exception message tells you have 'nul' instead of null/true/false/NaN values in your input:

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

So, pareser just can't map this value to actionDate property in your DTO. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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