2016-11-05 4 views
0

처음으로 Jackson을 시작하면서 JSON 데이터를 Java 클래스와 매핑하는 데 문제가 있습니다. json으로 구조는 다음과 같다 :Jackson Json과 POJO 매핑

{ 
    "status": "ok", 
    "all_tags": [ 
    { 
     "id": 14, 
     "term_name": "Term 1", 
     "category_details": [ 
     { 
      "category_ID": 21, 
      "category_name": "Category Name", 
      "category_count": 1, 
      "category_image": "...202x300.jpg" 
     }, 
     { 
      "category_ID": 19, 
      "category_name": "Category Sample", 
      "category_count": 3, 
      "category_image": "...202x300.jpg" 
     } 
     ] 
    }, 
    { 
     "id": 17, 
     "term_name": "Term 2", 
     "category_details": [ 
     { 
      "category_ID": 20, 
      "category_name": "Category Sample Again", 
      "category_count": 1, 
      "category_image": "....200x300.jpg" 
     } 
     ] 
    }, 
    ....... 
    ] 
} 

나는 CategoryDetail.java, AllTag.java, Response.java POJO 클래스를 생성 Jsonschema2pojo 웹 사이트를 사용했다.

 mRequestQueue = JacksonNetwork.newRequestQueue(getApplicationContext()); 
     mRequestQueue.add(new JacksonRequest<AllTag>(Request.Method.GET, 
     "http://example.com/file.json", 
     new JacksonRequestListener<AllTag>() { 
      @Override 
      public void onResponse(AllTag response, int statusCode, VolleyError error) { 
       if (response != null) { 
        // I am not sure how to parse the JSON 
        // and map the data to POJO 
       } else { 
        Log.e(TAG, "An error occurred while parsing the data! Stack trace follows:"); 
        error.printStackTrace(); 
       } 
      } 

      @Override 
      public JavaType getReturnType() { 
       return SimpleType.construct(AllTag.class); 
      } 
     }) 
); 

내가 documentation 읽을 수 있지만 내가 어떻게 클래스 그래서 내가 할 수있는 루프로 JSON 데이터를 매핑하고 데이터를 retreive하는 아무 생각이 : 나는 온라인 JSON에 접근하고 있기 때문에

내가 요청을 만들기 위해 발리를 사용 후에. 잭슨 지식을 가진 사람이 나를 올바른 방향으로 보내면 정말 고맙습니다.

답변

0

당신은 snakecase도 하나 더 클래스 reponse를 읽을 잭슨 필요

public class Response { 
    String status; 
    List<AllTags> allTags; 
    //setter getter 
    // constructor 
} 

업데이트 요청

 mRequestQueue = JacksonNetwork.newRequestQueue(getApplicationContext()); 
    mRequestQueue.add(new JacksonRequest<Response>(Request.Method.GET, 
    "http://example.com/file.json", 
    new JacksonRequestListener<Response>() { 
     @Override 
     public void onResponse(Response response, int statusCode, VolleyError error) { 
      if (response != null) { 
       // response.getAllTags() loop here 
       // I am not sure how to parse the JSON 
       // and map the data to POJO 
      } else { 
       Log.e(TAG, "An error occurred while parsing the data! Stack trace follows:"); 
       error.printStackTrace(); 
      } 
     } 

     @Override 
     public JavaType getReturnType() { 
      return SimpleType.construct(Response.class); 
     } 
    }) 
); 
귀하의 코멘트에 대한
+0

감사합니다, 내가 사용하고 [이 확장] (https : //로 GitHub의 .com/spothero/volley-jackson-extension). 그래서 응답 클래스가 필요하지 않습니까? –

+0

json 객체를 정확하게 나타내는 클래스 구조가 필요합니다. 이 https://github.com/spothero/volley-jackson-extension/blob/master/Demo/VolleyJacksonDemo/src/main/java/com/spothero/volleyjacksondemo/DateReturnType.java 및 http : //date.jsontest를 확인하십시오. co.kr/for volley-jackson-ext 응답 – Shashank

+0

에서 제공하는 질문에 게시 된 클래스 구조가 잘못 되었습니까? 같은 예제를 따랐지만 그 예제는 매우 간단하며 객체 배열을 저장하는 방법을 다루지 않습니다. –

관련 문제