Let's say I have a JSON file example.json
example.json
{
"BaggageMaxSize" : {
"mesurement" : "sum",
"width" : 70,
"height" : 50,
"depth" : 40
}
}
And create the POJO class:
public class pojoexample{
private BaggageMaxSize BaggageMaxSize;
// getter
// setter
}
And then:
public class BaggageMaxSize
{
private String height;
private String width;
private String depth;
private String mesurement;
// getter
// setter
}
Now, I want to use the mapper.readValue to change file to BaggageInfoPolicy.class:
BaggageInfoPolicy bip = mapper.readValue(file, BaggageInfoPolicy.class);
But bip.getBaggageMaxSize().getMesurement()
returns null value. Any suggestions?
Try using mapper.writeValue
first and check how your resulting JSON object will look like. Very likely, there's an issue with int -> string
conversion in your BaggageMaxSize
when deserialized from JSON.
Also, check your getters/setters to be publicly visible and be available both on pojoexample
and BaggageMaxSize
.
Actually your JSON represents a pojoexample
class instance and not a BaggageInfoPolicy
object, which you haven't shared in your post.
So you need to change your code to:
PojoExample bip = mapper.readValue(file, PojoExample.class);
So it reads the PojoExample
object correctly.
Note:
class
should follow the java naming convention and start with an uppercase, that's why I changed it to PojoExample
, change it in the class definition as well. types
as in the JSON, and their getters
and setters
are correctly implemented.
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.