简体   繁体   中英

Objectmapper.readValue does not deserialize correctly

I have a class:

class MyClass extends HashMap <K, V> {
@JsonProperty ("filed 1")
public String field1;
@JsonProperty ("field 2")
public String field2;}

when using:

ObjectMapper.readValue ("jsonString", MyClass.class)

all values are written to the map, but I need nothing to be written to the map, the json string stores only values for field1 and field2. What to do?

Its very easy to do like this... you use a class like this...

class MyClass {
@JsonProperty ("filed 1")
public String field1;

@JsonProperty ("field 2")
public String field2;
}

And then add a constructor to your HashMap based class, which takes MyClass as input and sets the values of the relevant fields correctly.

This way the code is equally good, but its easier to understand and maintain.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM