How To Make Pojo Class For Json
How should I make POJO class? Object may have unlimited array. This is my JSON, { 'result': { '0': [{ 'id': '51', 'first_name': 'ra',
Solution 1:
If you are trying to form json response schema for retrofit:
publicclassMyPojo
{
private String position;
privatenull 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;
}
publicvoidsetPosition (String position)
{
this.position = position;
}
publicnullgetDeleted_at ()
{
return deleted_at;
}
publicvoidsetDeleted_at (null deleted_at)
{
this.deleted_at = deleted_at;
}
public String getType ()
{
return type;
}
publicvoidsetType (String type)
{
this.type = type;
}
public String getStage ()
{
return stage;
}
publicvoidsetStage (String stage)
{
this.stage = stage;
}
public String getPassword ()
{
return password;
}
publicvoidsetPassword (String password)
{
this.password = password;
}
public String getId ()
{
return id;
}
publicvoidsetId (String id)
{
this.id = id;
}
public String getFirst_name ()
{
return first_name;
}
publicvoidsetFirst_name (String first_name)
{
this.first_name = first_name;
}
public String getTotal_childs ()
{
return total_childs;
}
publicvoidsetTotal_childs (String total_childs)
{
this.total_childs = total_childs;
}
public String getUpdated_at ()
{
return updated_at;
}
publicvoidsetUpdated_at (String updated_at)
{
this.updated_at = updated_at;
}
public String getEmail ()
{
return email;
}
publicvoidsetEmail (String email)
{
this.email = email;
}
public String getLast_name ()
{
return last_name;
}
publicvoidsetLast_name (String last_name)
{
this.last_name = last_name;
}
public String getCreated_at ()
{
return created_at;
}
publicvoidsetCreated_at (String created_at)
{
this.created_at = created_at;
}
public String getMobile_no ()
{
return mobile_no;
}
publicvoidsetMobile_no (String mobile_no)
{
this.mobile_no = mobile_no;
}
public String getParent_id ()
{
return parent_id;
}
publicvoidsetParent_id (String parent_id)
{
this.parent_id = parent_id;
}
}
Then in your api call:
Call<ResultResponse> xyz=interfacexyz.getResults(new Callback(...))
And you can define resultResponse as:
ResultRespone.java:
publicclassResultRespone
{
@SerializedName("results")
List<Map<string,MyPojo>> resultList;
// define getters and setters........
}
then you can access individual results as:
ResultResponse=response.body();
ResultResponse.get(0);//to get element at 0th position
Solution 2:
You have dynamic keys in your JSON as "1", "2", "3" and so on.. So you have to use conecpt of key. Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.
This example will help you to understand what do you want to do .
Post a Comment for "How To Make Pojo Class For Json"