2016-08-28 6 views
0

ObjectMapper 클래스로 중첩 된 json 문자열을 구문 분석 할 수 있습니다. 여기서 문제는 json을 java 객체로 구문 분석하는 최종 결과를 얻기 위해 많은 상용구 코드를 작성해야하므로 중첩 된 json을 구문 분석하기가 어려워졌습니다. 이 접근법 이외에 봄에 구문 분석의 더 좋은 방법이 있습니까?잭슨 봄 API를 사용하여 중첩 json을 구문 분석

도메인 클래스 :

구성 요소 클래스

public class Components { 

    private String name; 

    private String url; 

    private List<Properties> properties; 

    // getters and setters 
} 

속성 클래스 :

public class Properties { 

    private String name; 

    private String value; 

    private boolean exclusions; 

    // getters and setters 

} 

컨트롤러 클래스 :

@RestController 
public class TempController { 

    String jsonResponse = null; 

    @RequestMapping("/") 
    public @ResponseBody String getResponse(){ 
     JsonreadApplication ja = new JsonreadApplication(); 
     jsonResponse = ja.readJsonFile(); 
     //System.out.println(jsonResponse); 
     return jsonResponse; 
    } 

    @RequestMapping("/temp") 
    public @ResponseBody String getTempByServerType(String jsonResponse){ 
     jsonResponse = getResponse(); 
     Components components = new Components(); 
     ObjectMapper objectMapper = new ObjectMapper(); 
     try { 
      Iterator<Entry<String, JsonNode>> fieldNames = objectMapper.readTree(jsonResponse).fields(); 
      while(fieldNames.hasNext()){ 
       System.out.println(fieldNames.next()); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return "I've parsed json!"; 
    } 
} 

JSON 파일 :

{ 
    "name": "CatalogTools", 
    "url": "/atg/commerce/catalog/CatalogTools/", 
    "properties": [ 
     { 
      "name": "queryASAFFabrics", 
      "value": "skuType=\"ASAF_FABRIC\" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand", 
      "exclusions": "false" 
     }, 
     { 
      "name": "loggingDebug", 
      "value": "true", 
      "exclusions": "false" 
     } 
    ] 
} 

으로는 구문 분석 ObjectMapper 인스턴스와 수행하지만, 상용구 코드와 하드 코딩 JSON 요소 이름의 많은 수, 이전했다. 이 작업을 수행하는 더 좋은 방법이 있습니까? json 파일을 구문 분석하려면 jackson 라이브러리를 사용하고 있습니다.

나는 작업에 @RequestBody 주석이 사용 된 많은 온라인 링크를 언급했습니다. 여기서는 HTTP GET 요청입니다.

도움이 매우 감사합니다.

많은 분들께 미리 감사드립니다.

+0

public @ ResponseBody String getTempByServerType (@RequestBody Components 구성 요소)를 사용해 보셨습니까? – Andreas

+0

@RequestBody 주석은 http POST 작업에만 사용됩니다. 여기서는 GET 작업을 사용하고 있으며 내 질문에 대한 적절한 링크를 찾을 수 없습니다. 이 질문에 답할 수있는 곳에 참조 링크를 전달할 수 있다면 좋을 것입니다. – harshavmb

+0

GET 요청에서 복잡한 JSON (또는 JSON)을 쿼리 매개 변수로 * 보내지 * 않아야합니다. 주된 이유 : [URL의 크기가 제한됩니다] (http://stackoverflow.com/q/417142/5221149). 그러나 꼭해야만한다면'readTree()'대신'readValue()'를 시도 했습니까? – Andreas

답변

0

jsonchema2pojo을 사용할 수 있습니다.

json의 예를 들어 "Source Type"을 JSON으로 설정하고 Annotation 스타일을 Jackson 2.x 또는 1.x로 선택한 다음 "Zip"을 클릭하십시오. Voila, 전체 모델이 생성됩니다!

+0

많은 질문에 대한 답변 주셔서 감사합니다. 정말 멋지다. 봄 API에 대한 몇 가지 샘플 코드 조각이 있습니까? – harshavmb

+0

@harshavmb 아니요. – slanecek

+0

probs .. 다시 한번 감사드립니다! – harshavmb

관련 문제