如何将任意JSON字符串反序列化为Java POJO?

如何将任意JSON字符串反序列化为Java POJO?

问题描述:

让我们说我们有简单的json字符串json = {"key1":"value1", "key2":"value2"}和java类

Lets say we have simple json string json = {"key1":"value1", "key2":"value2"} and java class

class Foo {
private String field1;
private Integer field2;
//setter & getter
}

此外,我们不想更改Foo类.请注意,json键与Foo的字段名称不匹配. 有没有简单的方法可以用Jackson或任何其他库将json字符串反序列化为Foo类?

Moreover we don't want to change the Foo class. Note that json keys don't match with Foo's fields name. Is there simple way we can deserilize json string to Foo class with Jackson or any other library?

您可以使用以下json库并构建自定义解串器,如下所示.

You can use the following json libraries and build a custom deserializer as shown below.

jackson-annotations-2.10.4,
jackson-core-2.10.4,
jackson.databind-2.10.4

jackson-annotations-2.10.4,
jackson-core-2.10.4,
jackson.databind-2.10.4

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.IntNode;

import java.io.IOException;

public class FooDeserializer extends StdDeserializer<Foo> {


    public static void main (String [] args) throws JsonProcessingException {

        String json = "{\"key1\":\"value1\", \"key2\":100}";
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Foo.class, new FooDeserializer());
        mapper.registerModule(module);

        Foo foo = mapper.readValue(json, Foo.class);

        System.out.println(foo);
    }


    public FooDeserializer() {
        this(null);
    }

    public FooDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Foo deserialize(JsonParser jp, DeserializationContext ctx)
            throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String field1 = node.get("key1").asText();
        int field2 = (Integer) ((IntNode) node.get("key2")).numberValue();

        return new Foo(field1,field2);
    }
}