I have the following JSON structure:
{
"uri": {
"{{firstname}}": "Peter",
"{{lastname}}": "Griffin",
"{{age}}": 42
}
}
I want to deserialize it into my Bean:
public class Uri {
private String firstname;
private String lastname;
private int age;
/* getter and setter */
}
But I get the following error:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "uri" (class com.abc.Uri), not marked as ignorable (3 known properties: "firstname", "lastname", "age")
So I assume, I need to get into the property uri
.
Is there any way, to start parsing directly within the uri
property?
Update:
This is how I read the JSON:
ObjectMapper mapper = new ObjectMapper();
uri = mapper.readValue(new URL("test2.json"), Uri.class);
You JSON should be of the format:
{
"uri": {
"firstname": "Peter",
"lastname": "Griffin",
"age": 42,
}
}
Your method will not work because you are trying to get the whole json object at once, without getting a specific node at first.
Instead of loading your json with the mapper constructor, get your json in a different way. I would use URL
and HTTPURLConnection
to get the json string from the web.
After you have your json string, use this:
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(json);
Get the json node that uri
represents, like this:
JsonNode uriNode = rootNode.get("uri");
And only then send that node to be parsed, like this:
Uri uri = objectMapper.treeToValue(uriNode, Uri.class);
Uri uri = objectMapper.readValue(uriNode, Uri.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.