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

Puru
  • 31
  • 3
  • 1
    Does this answer your question? [Does Jackson deserialise the second character to lowercase for a property](https://stackoverflow.com/questions/43145150/does-jackson-deserialise-the-second-character-to-lowercase-for-a-property) – Joe Jun 17 '21 at 11:50

3 Answers3

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)

josejuan
  • 9,338
  • 24
  • 31
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);
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
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;
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47