-2

my JSON String :

{ "categorySetName": "TSN", "rows": [ { "date": 201750, "rank1": { "label": "\uBC30\uACBD", "search_keyword": "\uBC30\uACBD", "frequency": 3975, "score": 69.27185 }, "rank2": { "label": "\uBC30\uACBD\uD654\uBA74", "search_keyword": "\uBC30\uACBD\uD654\uBA74", "frequency": 3736, "score": 109.83768 }, "rank3": { "label": "\uC544\uC774\uD3F0x", "search_keyword": "\uC544\uC774\uD3F0x", "frequency": 3382, "score": 62.057728 }, . . . . "rank500": { "label": "\uC544\uC774\uD3F0x", "search_keyword": "\uC544\uC774\uD3F0x", "frequency": 1572, "score": 68.057728 } } }

my Deserializer.Java


@SuppressWarnings("serial")
public class AssociationTopRowDeserializer extends StdDeserializer<AssociationTopRow> {

    public static final String DATE = "date";
    public static final List<String> knownFieldNames = Arrays.asList(DATE);

    public AssociationTopRowDeserializer() {
        this(null);
    }

    public AssociationTopRowDeserializer(Class<AssociationTopRow> c) {
        super(c);
    }

    @Override
    @SuppressWarnings("unchecked")
    public AssociationTopRow deserialize(JsonParser jsonParser, DeserializationContext desContext) throws IOException {
        AssociationTopRow row = new AssociationTopRow();
        JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
        ObjectMapper objectMapper = new ObjectMapper();

        row.setDate(jsonNode.get(DATE).asText());

        String unknownField = getUnknownField(jsonNode.fieldNames());
        if (unknownField != null) {
            Map<String, AssociationTopRowDetail> map = new HashMap<String, AssociationTopRowDetail>();
            map.put(unknownField, (AssociationTopRowDetail) objectMapper.convertValue(jsonNode.get(unknownField),
                    AssociationTopRowDetail.class));
            row.setTransMap(map);
        }
        return row;
    }

    public String getUnknownField(Iterator<String> fieldNames) {
        while (fieldNames.hasNext()) {
            String next = fieldNames.next();
            if (!knownFieldNames.contains(next))
                return next;
        }
        return null;
    }
}

This shows me only information about only 'rank1'

2

1 Answer 1

0

You can use @JsonAnySetter annotation instead of a custom deserializer:

public class AssociationTopRow {

    private String date;
    private final Map<String, AssociationTopRowDetail> details = new LinkedHashMap<>();

    // getter & setter for date

    @JsonAnySetter
    public void addDetail(String key, AssociationTopRowDetail detail) {
        details.put(key, detail);
    }

    @JsonAnyGetter
    public Map<String, AssociationTopRowDetail> getDetails() {
        return details;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.