어떻게 동적으로 잭슨을 사용하여 하위 개체에 대한 키 값의 JSON 배열을지도로?

Carmageddon :

우리는 JSON 구조를 가지고 말 그 다음과 같다 :

{
 "field1": "val1",
 "field2": "v2",
 "f3": "v3",
 "f4": "v4",
 "arrayOfStuff": [
  {
   "f5": "v5",
   ....
   "f10": "v10"
  }
 ],
 "attributes": [
  {"att1": "att1"},
  {"att2": "attr2"},
  {"att3": "att3"}
 ],
 "options": [
  "ignoreMismatchFile"
 ]
}

그리고 우리의 일치 자바 클래스 외모가 좋아 :

public class Message {
   @IsUniqueId
   private String field1; //
   private String field2;
   private String field3;
   private String field4;
   private List<AnotherObject> f5;
   @JsonProperty("attributes")
   private LinkedHashMap<String, String> attributes;
   private List<String> options;
   ....
}

구문 분석 코드 외모 좋아 :

protected Message loadSavedMessageAsMessageObject(String path) throws IOException {
    File file = ResourceUtils.getFile(path);
    if (file.exists()) {
        ObjectMapper mapper = this.getObjectMapper();
        return mapper.readValue(file, Message.class);
    }

    return null;
}

우리는이 달성의 다른 방법을 시도, 원래 우리는 같은 속성을 가지고 노력 private List<MessageAttribute> attributes;(우리가에 따라지도로 전환하지만, 그 중 하나가 작동하지 않은 또 다른 답 -이 작동하지 않음)

킵은 동적 속성이 아닌 하드 코딩 된 목록 속성에 우리의 목표입니다.

어떻게 '입니다 MessageAttribute클래스처럼 보였다 :

public class MessageAttribute {
    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

현재 우리가 얻을는 예외입니다 :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_OBJECT token
at [Source: (File); line: 32, column: 3] (through reference chain: com.org.Message["attributes"])
데드 풀 :

해당 MessageJSON은 잘못된 형식입니다 이상으로 POJO를, 나는 변화의 부부가 만든 attributes해야 List of Map하고 목록 AnotherObject을 가리켜 야합니다arrayOfStuff

public class Message {
 @IsUniqueId
 private String field1; //
 private String field2;
 private String field3;
 private String field4;
 private List<AnotherObject> arrayOfStuff;  //or you can have List<Map<String,String>> arrayOfStuff
 @JsonProperty("attributes")
 private List<LinkedHashMap<String, String>> attributes; // this is list of map objects
 private List<String> options;
  ....
  }

추천

출처http://43.154.161.224:23101/article/api/json?id=181484&siteId=1