2016-10-26 25 views
-4

我應該如何製作POJO課程?對象可能有無限數組。如何爲JSON製作POJO課程

這是我JSON

{ 
    "result": { 
     "0": [{ 
      "id": "51", 
      "first_name": "ra", 
      "last_name": "d", 
      "email": "[email protected]", 
      "password": "1234", 
      "mobile_no": "8252536365", 
      "parent_id": "50", 
      "position": "0", 
      "type": "User", 
      "created_at": "1476447434", 
      "updated_at": "1476447434", 
      "deleted_at": null, 
      "stage": 0, 
      "total_childs": 0 
     }, { 
      "id": "52", 
      "first_name": "Ashish", 
      "last_name": "Chauhan", 
      "email": "[email protected]", 
      "password": "12345", 
      "mobile_no": "89889989832", 
      "parent_id": "8", 
      "position": "1", 
      "type": "admin", 
      "created_at": "1476702542", 
      "updated_at": "1476702542", 
      "deleted_at": null, 
      "stage": 0, 
      "total_childs": 0 
     }], 
     "2": [{ 
      "id": "2", 
      "first_name": "Ashish", 
      "last_name": "Chauhan", 
      "email": "[email protected]", 
      "password": "12345", 
      "mobile_no": "89889989832", 
      "parent_id": "1", 
      "position": "0", 
      "type": "admin", 
      "created_at": "1475674631", 
      "updated_at": "1475674631", 
      "deleted_at": null, 
      "stage": 2, 
      "total_childs": 2 
     }], 
     "1": [{ 
      "id": "7", 
      "first_name": "Shiva", 
      "last_name": "Singh", 
      "email": "[email protected]", 
      "password": "12345", 
      "mobile_no": "89889989832", 
      "parent_id": "2", 
      "position": "0", 
      "type": "user", 
      "created_at": "1475674808", 
      "updated_at": "1475674808", 
      "deleted_at": null, 
      "stage": 1, 
      "total_childs": 2 
     }, { 
      "id": "8", 
      "first_name": "Atul", 
      "last_name": "Kumar", 
      "email": "[email protected]", 
      "password": "12345", 
      "mobile_no": "89889989832", 
      "parent_id": "2", 
      "position": "1", 
      "type": "user", 
      "created_at": "1475674835", 
      "updated_at": "1475674835", 
      "deleted_at": null, 
      "stage": 1, 
      "total_childs": 2 
     }] 
    } 
} 
+0

你到底想幹什麼?你遇到什麼樣的問題? – 0xDEADC0DE

+1

[如何創建一個POJO?](http://stackoverflow.com/questions/3527264/how-to-create-a-pojo) –

回答

0

您的JSON動態密鑰爲「1」,「2」,「3」等。所以,你必須使用關鍵的conecpt。 使用JSONObject keys()獲取密鑰,然後迭代每個密鑰以獲得動態值。

This示例將幫助您瞭解您想要做什麼。

1

如果你正在嘗試形成改造JSON響應模式:

public class MyPojo    
{ 
    private String position; 

    private null deleted_at; 

    private String type; 

    private String stage; 

    private String password; 

    private String id; 

    private String first_name; 

    private String total_childs; 

    private String updated_at; 

    private String email; 

    private String last_name; 

    private String created_at; 

    private String mobile_no; 

    private String parent_id; 

    public String getPosition() 
    { 
     return position; 
    } 

    public void setPosition (String position) 
    { 
     this.position = position; 
    } 

    public null getDeleted_at() 
    { 
     return deleted_at; 
    } 

    public void setDeleted_at (null deleted_at) 
    { 
     this.deleted_at = deleted_at; 
    } 

    public String getType() 
    { 
     return type; 
    } 

    public void setType (String type) 
    { 
     this.type = type; 
    } 

    public String getStage() 
    { 
     return stage; 
    } 

    public void setStage (String stage) 
    { 
     this.stage = stage; 
    } 

    public String getPassword() 
    { 
     return password; 
    } 

    public void setPassword (String password) 
    { 
     this.password = password; 
    } 

    public String getId() 
    { 
     return id; 
    } 

    public void setId (String id) 
    { 
     this.id = id; 
    } 

    public String getFirst_name() 
    { 
     return first_name; 
    } 

    public void setFirst_name (String first_name) 
    { 
     this.first_name = first_name; 
    } 

    public String getTotal_childs() 
    { 
     return total_childs; 
    } 

    public void setTotal_childs (String total_childs) 
    { 
     this.total_childs = total_childs; 
    } 

    public String getUpdated_at() 
    { 
     return updated_at; 
    } 

    public void setUpdated_at (String updated_at) 
    { 
     this.updated_at = updated_at; 
    } 

    public String getEmail() 
    { 
     return email; 
    } 

    public void setEmail (String email) 
    { 
     this.email = email; 
    } 

    public String getLast_name() 
    { 
     return last_name; 
    } 

    public void setLast_name (String last_name) 
    { 
     this.last_name = last_name; 
    } 

    public String getCreated_at() 
    { 
     return created_at; 
    } 

    public void setCreated_at (String created_at) 
    { 
     this.created_at = created_at; 
    } 

    public String getMobile_no() 
    { 
     return mobile_no; 
    } 

    public void setMobile_no (String mobile_no) 
    { 
     this.mobile_no = mobile_no; 
    } 

    public String getParent_id() 
    { 
     return parent_id; 
    } 

    public void setParent_id (String parent_id) 
    { 
     this.parent_id = parent_id; 
    } 

} 

然後在您的API調用:

Call<ResultResponse> xyz=interfacexyz.getResults(new Callback(...)) 

而且你可以定義resultResponse爲:

ResultRespone。 java:

public class ResultRespone 
{ 
    @SerializedName("results") 
    List<Map<string,MyPojo>> resultList; 
    // define getters and setters 
    .... 
    .... 
} 

那麼你就可以訪問各個結果:

ResultResponse=response.body(); 
ResultResponse.get(0);//to get element at 0th position