Reputation: 29
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"])
Upvotes: 0
Views: 483
Reputation: 4582
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;
Upvotes: 0
Reputation: 1606
You can set Fail on unknown properties as false to avoid this issue
ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Upvotes: 0
Reputation: 9566
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)
Upvotes: 1