1

私は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);

のすべてのプロパティpartTypenull. ここで私が間違っていることと、これを解決する方法は?

4

2 に答える 2

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") 
于 2013-02-07T10:43:21.630 に答える
0

Json ヘッダーが間違っています。使用する

@JsonTypeName("evaluationPart") 

それ以外の

@JsonIgnoreProperties(value = {"evaluationPart"})
于 2013-02-07T10:41:46.707 に答える