0

I am trying to convert string into json using jackson.

I have used ObjectMapper().readValue() and deserialize into a DragDropJsonDto class

qList.getHeader() is like this "<p>{
 "RC" : ["N" , "Raj" , "USA"], 
"LC" : [ 
"Enter Country Name :" , 
"Enter State Name :", 
"Enter City Name :" ]
 }</p>"

public class DragDropJsonDto {

private List<String> RC;
private List<String> LC;

public List<String> getRC() {
    return RC;
}

public void setRC(List<String> RC) {
    this.RC = RC;
}

public List<String> getLC() {
    return LC;
}

public void setLC(List<String> LC) {
    this.LC = LC;
}

}

DragDropJsonDto dragDropJson = new ObjectMapper().readValue(qList.getHeader(), DragDropJsonDto.class);

I am not able to convert into json exception arises Error Unrecognized field "RC" (class com.visataar.lt.web.dto.DragDropJsonDto), not marked as ignorable (2 known properties: "rc", "lc"])

1

3 Answers 3

1

By default jackson use lowercase, if you need RC and LC use:

private class DragDropJsonDto {
    ...
    @JsonProperty("RC")
    private List<String> RC;
    @JsonProperty("LC")
    private List<String> LC;
    ...
}

(or in getters)

Sign up to request clarification or add additional context in comments.

Comments

0

You can set Fail on unknown properties as false to avoid this issue

ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Comments

0

As the qList.getHeader() response String Object Deserialization has 2 issues:

i) Response JSON String contains <p> and </p>. So you need to remove these sub-strings. As below:

String objectJson = qList.getHeader().replace("<p>", "").replace("</p>", "");
DragDropJsonDto f = m.readValue(objectJson, DragDropJsonDto.class);

ii) JSON String is having Keys in capital letters. As by default jackson uses small letters you need to use JsonProperty in DTO. As below:

  @JsonProperty("RC") 
  private List<String> RC;
  @JsonProperty("LC") 
  private List<String> LC;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.