2016-11-05 113 views
0

傑克遜第一次入門,我有問題與Java類映射JSON數據。該JSON結構如下:傑克遜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" 
     } 
     ] 
    }, 
    ....... 
    ] 
} 

我以前Jsonschema2pojo網站產生CategoryDetail.javaAllTag.javaResponse.java POJO類。

因爲我訪問JSON在線我用凌空提出要求:

 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數據後來。真的很感謝傑克遜知識的人帶我走向正確的方向。

回答

0

您需要傑克遜閱讀snakecase,也多了一個類效應初探

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。 com /供參考在排列傑克遜分機響應 – Shashank

+0

提供,所以我在問題中發佈的類結構是不正確的?我遵循同一個例子,但這個例子非常簡單,並不包括如何存儲對象數組。 –