2012-08-30 4 views
0

이 JSON 비트를 POJO로 가져 오는 데 문제가 있습니다. 여기 JSON 목록을 POJO로 변환

protected ThreadLocal<ObjectMapper> jparser = new ThreadLocal<ObjectMapper>(); 

    public void receive(Object object) { 
    try { 
     if (object instanceof String && ((String)object).length() != 0) { 
      ObjectDefinition t = null ; 
      if (parserChoice==0) { 
       if (jparser.get()==null) { 
        jparser.set(new ObjectMapper()); 
       } 
       t = jparser.get().readValue((String)object, ObjectDefinition.class); 
      } 

      Object key = t.getKey(); 
      if (key == null) 
       return; 
      transaction.put(key,t); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

는 POJO로 전환 할 필요가있는 JSON이다 : 나는 잭슨은 다음과 같이 구성되어 사용하고

{ 
    "id":"exampleID1", 
    "entities":{ 
     "tags":[ 
     { 
      "text":"textexample1", 
      "indices":[ 
       2, 
       14 
      ] 
     }, 
     { 
      "text":"textexample2", 
      "indices":[ 
       31, 
       36 
      ] 
     }, 
     { 
      "text":"textexample3", 
      "indices":[ 
       37, 
       43 
      ] 
     } 
     ] 
} 

그리고 마지막으로, 여기에 내가 현재 자바 클래스에있는 내용은 다음과 같습니다

protected Entities entities; 
@JsonIgnoreProperties(ignoreUnknown = true) 
protected class Entities { 
    public Entities() {} 
    protected Tags tags; 
    @JsonIgnoreProperties(ignoreUnknown = true) 
    protected class Tags { 
     public Tags() {} 

     protected String text; 

     public String getText() { 
      return text; 
     } 

     public void setText(String text) { 
      this.text = text; 
     } 
    }; 

    public Tags getTags() { 
     return tags; 
    } 
    public void setTags(Tags tags) { 
     this.tags = tags; 
    } 
}; 
//Getters & Setters ... 

더 간단한 개체를 POJO로 변환 할 수 있었지만 그 목록은 저를 곤혹스럽게 만들었습니다.

도움을 주시면 감사하겠습니다. 감사!

+1

http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – mre

답변

3

문제가 클래스 정의에 있다고 생각합니다. Tags 클래스에 배열 인 Json의 원시 텍스트가 포함되기를 바랍니다. 내가 JSON 배열을 표현하기 위해 목록을 사용하여 필드 태그에 다음

protected Entities entities; 
@JsonIgnoreProperties(ignoreUnknown = true) 
protected class Entities { 
    public Entities() {} 
    @JsonDeserialize(contentAs=Tag.class) 
    protected List<Tag> tags; 

    @JsonIgnoreProperties(ignoreUnknown = true) 
    protected class Tag { 
     public Tag() {} 

     protected String text; 

     public String getText() { 
      return text; 
     } 

     public void setText(String text) { 
      this.text = text; 
     } 
    }; 

    public Tags getTags() { 
     return tags; 
    } 
    public void setTags(Tags tags) { 
     this.tags = tags; 
    } 
}; 

을, 나는 태그 클래스와 그 목록의 내용을 역 직렬화하는 잭슨에게 : 내가 대신 어떻게 할 것인지. 이것은 Jackson이 일반 선언의 런타임 정보를 가지고 있지 않기 때문에 필요합니다. 인덱스에 대해서도 똑같은 작업을 수행합니다. 즉, JsonDeserialize 주석이있는 List<Integer> indices 필드가 있습니다.

+0

감사합니다. –

+0

또한이 클래스를 작동 시키려면이 두 클래스를 정적으로 만들어야했습니다. 여기 저를 이끌었습니다 : http://stackoverflow.com/questions/8526333/jackson-error-no-suitable-constructor –

+1

좋아. 어노테이션 중 하나 (@JsonDeserialize (contentAs =))는 중복됨을 유의하십시오. 상처는 없지만 필요하지는 않습니다 (필드 서명이 모든 정보를 가지고 있기 때문에). – StaxMan