2012-11-27 3 views
1

비교적 복잡한 JSON을 Jackson 라이브러리와 구문 분석해야합니다. 자바 클래스 구조와 관련하여 다음 JSON 객체를 구문 분석하는 데 Jackson이 접근하는 방법에 대해 조언 해 주실 수 있겠습니까?복잡한 JSON 객체와 Jackson의 구문 분석

{ 
    "firstField": "Something One", 
    "secondField": "Something Two", 
    "thirdField": [ 
    { 
     "thirdField_one": "Something Four", 
     "thirdField_two": "Something Five" 
    }, 
    { 
     "thirdField_one": "Something Six", 
     "thirdField_two": "Something Seven" 
    } 
    ], 
    "fifthField": [ 
    { 
     "fifthField_one": "Something… ", 
     "fifthField_two": "Something...", 
     "fifthField_three": 12345 
    }, 
    { 
     "fifthField_one": "Something", 
     "fifthField_two": "Something", 
     "fifthField_three": 12345 
    } 
    ] 
} 
+0

당신은 잭슨을 사용하여 솔루션을 얻었 는가? – andro

답변

1

좀 더 GSON에 조예가있어,하지만 난이 일을해야한다고 생각 :

그것은 이렇게이다. 아래

참조 StaxMan 주 :

잭슨이 자동으로 private 필드를 감지하지 않습니다는 (그것은 @JsonAutoDetect 또는 세계적으로 구성 할 수 있습니다). 따라서 필드는 비공개이거나 @JsonProperty로 주석을 달거나 일치하는 공용 getter를 가져야합니다.

public class MyClass { 
    private String firstField, secondField; 
    private ThirdField thirdField; 
    private FifthField fifthField; 

    public static class ThirdField { 
     private List<ThirdFieldItem> thirdField; 
    } 

    public static class ThirdFieldItem { 
     private String thirdField_one, thirdField_two; 
    } 

    public static class FifthField { 
     private List<FifthFieldItem> fifthField; 
    } 

    public static class FifthField { 
     private String fifthField_one, fifthField_two; 
     private int fifthField_three; 
    } 
} 
+0

Gson과 달리 Jackson은 자동으로 개인 필드를 감지하지 않습니다 (@JsonAutoDetect 또는 전역으로 구성 할 수 있음). 따라서 필드는 비공개이거나'@JsonProperty' 주석이 있거나 public getter와 일치해야합니다. – StaxMan

+0

아, StaxMan, 잘 알려 줘서 고마워. 나는 대답을 업데이트 할 것이다. – antew