I have a third party API that gives response as JSON object whose variables has first letter capital My JSON object looks like this:
{
"ApiStatus":{
"ApiStatusCode":5000,
"ApiMessage":"OK",
"FWStatusCode":0,
"FWMessage":""
},
"Encounter":{
"EncounterId":"hgasfdjsdgkf",
"ApiLinks":[
{
"Title":"Self",
"Description":"Self referencing api",
"ResourceName":"self",
"HttpMethods":[
"GET",
"DELETE"
],
"URL":"http://www.google.com/api/v2/test/test2/hchvjh"
},
{
"Title":"Transmit a Report",
"Description":"Create a report and transmit it.",
"ResourceName":"transmitter",
"HttpMethods":[
"POST"
],
"URL":"http://www.google.com/api/v2/test3/test3/jydgfkshd/transmitter"
}
]
}
}
As you can see above variable's first letter is capital and need to map that in My java object class, so when I tried to use ObjectMapper
, but it's not working.
MyJavaObject object = new ObjectMapper().readValue(jsonObject, MyJavaObject.class);
Any Suggestion or help would be great.
CodePudding user response:
You need to set the default naming strategy for Jackson to be UpperCamelCaseStrategy
as follows:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UpperCamelCaseStrategy);
MyJavaObject object = objectMapper.readValue(jsonObject, MyJavaObject.class);
For the properties URL
, FWStatusCode
and FWMessage
, which do not follow camel case, snake case or any other naming strategy, you will you need to use @JsonProperty
on your class attributes so that Jackson knows how to handle them. Just annotate the class attribute with the respective @JsonProperty
and it will work:
@JsonProperty("URL")
@JsonProperty("FWStatusCode")
@JsonProperty("FWMessage")