2

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

glen maxwell
  • 193
  • 1
  • 3
  • 13
  • I've tried above code with oracle jdk 1.8 and jackson-databind 2.6.5 and it works well for me. Could you provide more details about your environment. – Maksym Prokhorenko Mar 25 '17 at 08:44

2 Answers2

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.

0

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.

Feezan Khattak
  • 212
  • 3
  • 15