我的JSON字符串:
{ " categorySetName":" TSN", "行":[ { " date":201750, " rank1":{ "标签":" \ uBC30 \ uACBD", " search_keyword":" \ uBC30 \ uACBD", "频率":3975, "得分":69.27185 }, " rank2":{ "标签":" \ uBC30 \ uACBD \ uD654 \ uBA74", " search_keyword":" \ uBC30 \ uACBD \ uD654 \ uBA74", "频率":3736, "得分":109.83768 }, " rank3":{ "标签":" \ uC544 \ uC774 \ uD3F0x", " search_keyword":" \ uC544 \ uC774 \ uD3F0x", "频率":3382, "得分":62.057728 }, 。 。 。 。 " rank500":{ "标签":" \ uC544 \ uC774 \ uD3F0x", " search_keyword":" \ uC544 \ uC774 \ uD3F0x", "频率":1572, "得分":68.057728 } } }
我的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;
}
}
这只显示了有关&#39; rank1&#39;的信息。
答案 0 :(得分:0)
您可以使用@JsonAnySetter
注释而不是自定义反序列化器:
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;
}
}