使用Jackson 2将JSON反序列化为Java

时间:2013-02-07 10:35:47

标签: java json jackson

我有一个JSON:

{"evaluationPart": {
    "generatedId": "48D5181DB8704F8AB5FC998964AD9075",
    "evaluationQuestionPartOption": {
        "generatedId": "48D5181DB8704F8AB5FC998964AD9075"
    }
}}

我为它创建了java类来表示它:

根类:

@JsonIgnoreProperties(value = {"evaluationPart"})
public class JsonEvaluationPart {

    @JsonProperty("generatedId")
    private String generatedId;

    @JsonProperty("evaluationQuestionPartOption")
    private JsonQuestionOption questionOption;

    public String getGeneratedId() {
        return generatedId;
    }

    public void setGeneratedId(String generatedId) {
        this.generatedId = generatedId;
    }

    public JsonQuestionOption getQuestionOption() {
        return questionOption;
    }

    public void setQuestionOption(JsonQuestionOption questionOption) {
        this.questionOption = questionOption;
    }
}

JsonQuestionOption类:

public class JsonQuestionOption {

    @JsonProperty("generatedId")
    private String generatedId;

    public String getGeneratedId() {
        return generatedId;
    }

    public void setGeneratedId(String generatedId) {
        this.generatedId = generatedId;
    }
}

我写了一个小的JUnit测试来检查它是怎么回事:

public class JsonReaderTest {

    /**
     * Logger for this class.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonReaderTest.class);

    private ObjectMapper objectMapper;

    private static final String JSON = "{\"evaluationPart\": {\n" +
            "    \"generatedId\": \"48D5181DB8704F8AB5FC998964AD9075\",\n" +
            "    \"evaluationQuestionPartOption\": {\n" +
            "        \"generatedId\": \"48D5181DB8704F8AB5FC998964AD9075\"\n" +
            "    }\n" +
            "}}";

    @Before
    public void setUp()
            throws Exception {
        LOGGER.debug("Creating the object mapper.");
        objectMapper = new ObjectMapper();
        LOGGER.debug("Object mapper successfully created. {}", objectMapper);
    }

    @Test
    public void testJsonReader()
            throws Exception {

        JsonEvaluationPart partType = objectMapper.readValue(JSON, JsonEvaluationPart.class);
        assertNotNull(partType);

        LOGGER.debug("Part: {}.", partType);

        assertEquals(partType.getGeneratedId(), "48D5181DB8704F8AB5FC998964AD9075");
        assertEquals(partType.getQuestionOption().getGeneratedId(), "48D5181DB8704F8AB5FC998964AD9075");
    }

}

问题在于,当我正在阅读我的JSON时:

JsonEvaluationPart partType = objectMapper.readValue(JSON, JsonEvaluationPart.class);

partType中的所有媒体资源均为null。我在这里做错了什么以及如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

根据文档JsonIgnoreProperties表示:

Annotation that can be used to either suppress serialization of properties 
(during serialization), or ignore processing of JSON properties read (during 
deserialization).

试试替换:

@JsonIgnoreProperties(value = {"evaluationPart"}) 

使用:

@JsonTypeName("evaluationPart") 

答案 1 :(得分:0)

你的Json标题是错误的。 使用

@JsonTypeName("evaluationPart") 

而不是

@JsonIgnoreProperties(value = {"evaluationPart"})