如何使用jackson objectmapper反序列化

时间:2018-03-08 06:31:41

标签: java json jackson

我想将json(如下所示)转换为java对象。我现在为json对象创建了java类,而不使用任何jackson注释。

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJunkie {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        String json = "{\r\n" + 
                "   \"Info\":{\r\n" + 
                "       \"prop1\": \"value1\",\r\n" + 
                "       \"prop2\": \"value2\",\r\n" + 
                "       \"prop3\": \"value3\"\r\n" + 
                "   },\r\n" + 
                "   \"Data\":{\r\n" + 
                "       \"prop1\": \"value1\",\r\n" + 
                "       \"prop2\": \"value2\"\r\n" + 
                "   }\r\n" + 
                "}";

        Pack pack = objectMapper.readValue(json, Pack.class);
        System.out.println(pack);
    }

}

我将上面的Json对象转换为一个名为" Pack"的Java类。下面:

import org.apache.commons.lang3.builder.ToStringBuilder;

public class Pack {

    private Info info;
    private Data data;

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("info", info).append("data", data).toString();
    }

}

我故意省略了Info和Data的类。他们的变量,getter,setter与json匹配。如果你愿意,我可以加入它们。

我收到以下异常。为什么会出现这种异常?我该如何解决?

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Info" (class com.tester.Jacksons.Pack), not marked as ignorable (2 known properties: "data", "info"])
 at [Source: (String)"{
    "Info":{
        "prop1": "value1",
        "prop2": "value2",
        "prop3": "value3"
    },
    "Data":{
        "prop1": "value1",
        "prop2": "value2"
    }
}"; line: 2, column: 10] (through reference chain: com.tester.Jacksons.Pack["Info"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1582)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1560)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
    at com.tester.Jacksons.TestJunkie.main(TestJunkie.java:22)

2 个答案:

答案 0 :(得分:0)

你的对象的键是小写的,在json中它们是上骆驼的大小写。 如果此命名方案一致,则可以在对象映射器上设置命名状态。

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UpperCamelCaseStrategy);

答案 1 :(得分:0)

Pack中的属性与名称不匹配,因为在json中它以大写字母开头。 因此,请按以下方式更新Pack

import org.apache.commons.lang3.builder.ToStringBuilder;

public class Pack {

    @JsonProperty("Info")
    private Info info;
    @JsonProperty("Data")
    private Data data;

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("info", info).append("data", data).toString();
    }

}

此外,如果存在Info或Data类中未指定的属性。在类上使用@JsonIgnoreProperties(ignoreUnknown = true)注释来忽略未知属性。

<强>更新

如果必须使用与Hibernate实体相同的类或在其他地方使用注释,则使用注释不是很方便。因此,我建议您执行serialization and deserialization using Dtos,然后将这些Dtos中的值放到您需要的其他对象中。您可以使用mapstruct等良好的api来创建对象。