2015-01-30 2 views
1
{ 
    "query":{ 
     "data":{ 
     "val1":{ 
      "id":123, 
      "name":"abc" 
     }, 
     "val2":{ 
      "id":13, 
      "name":"def" 
     }, 
     "total":{ 
      "count":2, 
      "page":{ 
       "num":1 
      } 
     } 
     } 
    } 
} 

내 json은 위처럼 보입니다. "val1", val2 "동적, 그래서 Map.Everything 내가"총 "태그를 얻을 때까지 잘 작동했습니다. 구조 때문에 개체 매핑이 실패합니다. "총 " 태그 구문 분석하거나 다른 개체에 "총"을 구문 분석하는 동안.Jackson을 Java POJO에 매핑하기 Jackson을 사용하여

나는 예외 아래 얻을

Exception in thread "main" java.lang.IllegalArgumentException: Instantiation of [simple type, class jsom.schema.pojo.Definitions] value failed: Unrecognized field "count" (Class jsom.schema.pojo.MainResourceObj), not marked as ignorable 
at [Source: [email protected]; line: 4, column: 26] (through reference chain: jsom.schema.pojo.Definitions["definitions"]->jsom.schema.pojo.MainResourceObj["count"]) 

내가 업데이트 내 POJO는 아래와 같이, 여전히 전체 태그 Map.which에 점점 예외

원인
HashMap<String, customObject> definitions; 
@JsonProperty("total") 
Total total; 

public class Total { 

    public Total() { 
     // TODO Auto-generated constructor stub 
    } 
    String count; 
    Page page; 
    public String getCount() { 
     return count; 
    } 
    public void setCount(String count) { 
     this.count = count; 
    } 
    public Page getPage() { 
     return page; 
    } 
    public void setPage(Page page) { 
     this.page = page; 
    } 

    @JsonCreator 
    public static Total Create(String jsonString) throws JsonParseException, 
      JsonMappingException, IOException { 
     ObjectMapper mapper = new ObjectMapper(); 
     Total module = null; 
     module = mapper.readValue(jsonString, Total.class); 
     return module; 
    } 

} 

답변

1

사용하고 싶다고 생각합니다. 변수 선언 위에 @JsonIgnore 태그가 있습니다. 그것을 밖으로보십시오!

@JsonIgnore 
Total total; 
+0

안녕하세요 colleen stock @ JsonIgnore 주석 트릭을하지 않았다, 여전히 같은 예외 – user2478236

+0

흠. 예외 메시지가 다른 문제를 지적하는 것처럼 보입니다. 게시물에서 언급 한 것처럼 "전체"태그를 건너 뛰려면 @JsonIgnore를 사용해야합니다. 예외 메시지에 "개수"가 인식되지 않는다고 표시됩니다. 그 결과 총계 클래스의 "카운트"멤버가 누락되었다고 생각하게됩니다. 문제를 해결하려면 추가해야 할 수도 있습니다. 전체 수업에서 이것을 시도하십시오 : 'public class Total { public int count; }' –

+0

Jakson이 해당 레벨의 모든 요소를 ​​Map 에 퍼팅하려고 했으므로 총 클래스를 추가했습니다. 객체가 다른 구조를 가지므로 예외가 발생합니다. – user2478236

1

당신이 @JsonIgnore 또는 @JsonIgnoreProperties 주석을 사용한다 속성 (일명 필드를) 무시합니다.

는 귀하의 코드는 다음 중 하나가 될 것입니다 : JsonIgnoreProperties를 사용

HashMap<String, customObject> definitions; 
@JsonProperty("total") 
Total total; 

또는

... 
@JsonIgnoreProperties({"total"}) 
... class ... { 
HashMap<String, customObject> definitions; 
Total total; 
... 

당신이 한 장소에서 하나 개 이상의 속성을 제외 할 수 있습니다.

+0

안녕하세요 David "@JsonIgnore"및 @JsonIgnoreProperties 주석이 트릭을 수행하지 않았지만 여전히 동일한 예외 – user2478236

+0

Total을 무시하려면 Query 클래스에 주석을 추가해야합니다. – David

관련 문제