简体   繁体   中英

How to get Complex JSON response into Array List? What to write in "on response" to get only records from json response?

How to get Complex JSON response into Array List?

I want to display only records from below json but as i am new i dont know how to store such json data into array list please guide me for below issue as i have searched many anwers but not able to make it work.

JSON Response

    {
"Status":200,
"Message":"Success",
"data":{
    "TotalRecords":10,
    "Records":[
                 {
                 "Id":1,
                 "title":"Smile Crowdfunding",
                 "shortDescription":"This foundation will bring smile on there faces",
                 "collectedValue":500,
                 "totalValue":5000,
                 "startDate":"05/05/2018",
                 "endDate":"10/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project1.jpg"
                 },
                 {
                 "Id":2,
                 "title":"Animal Funding",
                 "shortDescription":"This foundation will help animals",
                 "collectedValue":200,
                 "totalValue":10000,
                "startDate":"10/05/2018",
                "endDate":"11/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project2.jpg"
                 },
                 {
                 "Id":3,
                 "title":"Children Funding",
                 "shortDescription":"This foundation will bring smile on there faces",
                 "collectedValue":440,
                 "totalValue":4000,
                 "startDate":"25/05/2018",
                 "endDate":"15/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project3.jpg"
                 },
                 {
                 "Id":4,
                 "title":"Old Age Home Funding",
                 "shortDescription":"This foundation will help old age people",
                 "collectedValue":500,
                 "totalValue":10000,
                 "startDate":"23/05/2018",
                 "endDate":"13/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project4.jpg"
                 },
                 {
                 "Id":5,
                 "title":"Smile Crowdfunding",
                 "shortDescription":"This foundation will bring smile on there faces",
                 "collectedValue":2000,
                 "totalValue":50000,
                 "startDate":"05/05/2018",
                 "endDate":"14/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project5.jpg"
                 },
                 {
                 "Id":6,
                 "title":"Children Funding",
                 "shortDescription":"This foundation will help poor children",
                 "collectedValue":1200,
                 "totalValue":40000,
                 "startDate":"25/05/2018",
                 "endDate":"11/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project6.jpg"
                 },
                 {
                 "Id":7,
                 "title":"Smile Crowdfunding",
                 "shortDescription":"This foundation will bring smile on there faces",
                 "collectedValue":1000,
                 "totalValue":100000,
                 "startDate":"22/05/2018",
                 "endDate":"22/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project7.jpg"
                 },
                 {
                 "Id":8,
                 "title":"Animal Funding",
                 "shortDescription":"This foundation will help animals",
                 "collectedValue":500,
                 "totalValue":50000,
                 "startDate":"29/05/2018",
                 "endDate":"10/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project8.jpg"
                 },
                 {
                 "Id":9,
                 "title":"Rotary Club Funding",
                 "shortDescription":"This foundation will go to rotary club",
                 "collectedValue":200,
                 "totalValue":30000,
                 "startDate":"23/05/2018",
                 "endDate":"10/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project9.jpg"
                 },
                 {
                 "Id":10,
                 "title":"Animal Funding",
                 "shortDescription":"This foundation will help animals",
                 "collectedValue":750,
                 "totalValue":20000,
                 "startDate":"05/05/2018",
                 "endDate":"08/06/2018",
                 "mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project10.jpg"
                 }
             
             ]
    }
}

API interface

 public interface RequestInterface {
    @GET("/testdata.json")
    Call<ApiResponse> getApiJson();
}

MODAL CLASS:

public class ApiResponse {

    public class Record {

        @SerializedName("Id")
        @Expose
        private Integer id;
        @SerializedName("title")
        @Expose
        private String title;
        @SerializedName("shortDescription")
        @Expose
        private String shortDescription;
        @SerializedName("collectedValue")
        @Expose
        private Integer collectedValue;
        @SerializedName("totalValue")
        @Expose
        private Integer totalValue;
        @SerializedName("startDate")
        @Expose
        private String startDate;
        @SerializedName("endDate")
        @Expose
        private String endDate;
        @SerializedName("mainImageURL")
        @Expose
        private String mainImageURL;

        public Integer getId() {
            return id;
        }

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

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getShortDescription() {
            return shortDescription;
        }

        public void setShortDescription(String shortDescription) {
            this.shortDescription = shortDescription;
        }

        public Integer getCollectedValue() {
            return collectedValue;
        }

        public void setCollectedValue(Integer collectedValue) {
            this.collectedValue = collectedValue;
        }

        public Integer getTotalValue() {
            return totalValue;
        }

        public void setTotalValue(Integer totalValue) {
            this.totalValue = totalValue;
        }

        public String getStartDate() {
            return startDate;
        }

        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }

        public String getEndDate() {
            return endDate;
        }

        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }

        public String getMainImageURL() {
            return mainImageURL;
        }

        public void setMainImageURL(String mainImageURL) {
            this.mainImageURL = mainImageURL;
        }

    }

    public class Data{
        @SerializedName("TotalRecords")
        @Expose
        private Integer totalRecords;
        @SerializedName("Records")
        @Expose
        private List<Record> records = null;

        public Integer getTotalRecords() {
            return totalRecords;
        }

        public void setTotalRecords(Integer totalRecords) {
            this.totalRecords = totalRecords;
        }

        public List<Record> getRecords() {
            return records;
        }

        public void setRecords(List<Record> records) {
            this.records = records;
        }

    }

    public class Body {

        @SerializedName("Status")
        @Expose
        private Integer status;
        @SerializedName("Message")
        @Expose
        private String message;
        @SerializedName("data")
        @Expose
        private Data data;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Data getData() {
            return data;
        }

        public void setData(Data data) {
            this.data = data;
        }

    }

}

MainActivity

I am getting Success toast on running but now what to write in on response to store data in array...because i am getting whole apiresponse in on response.

    public class MainActivity extends AppCompatActivity {

    ArrayList<ApiResponse> apiResponseArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getApiResponse();
    }

    private void getApiResponse() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://testffc.nimapinfotech.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);
        Call<ApiResponse> call = requestInterface.getApiJson();

        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                //apiResponseArrayList.add(response.body());
                if (response.isSuccessful()){
                    
                    Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();
                    
                    /*try {
                        JSONObject obj = new JSONObject(response.toString());
                        JSONArray records = obj.getJSONArray("Records");
                        Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){

                    }*/
                    //apiResponseArrayList = new ArrayList<>(response.body());

                }
            }

            @Override
            public void onFailure(Call<ApiResponse> call, Throwable t) {
                Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Modify MODAL class to:

public class ApiResponce {

/**
 * Status : 200
 * Message : Success
 * data : {"TotalRecords":10,"Records":[{"Id":1,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":500,"totalValue":5000,"startDate":"05/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project1.jpg"},{"Id":2,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":200,"totalValue":10000,"startDate":"10/05/2018","endDate":"11/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project2.jpg"},{"Id":3,"title":"Children Funding","shortDescription":"This foundation will bring smile on there faces","collectedValue":440,"totalValue":4000,"startDate":"25/05/2018","endDate":"15/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project3.jpg"},{"Id":4,"title":"Old Age Home Funding","shortDescription":"This foundation will help old age people","collectedValue":500,"totalValue":10000,"startDate":"23/05/2018","endDate":"13/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project4.jpg"},{"Id":5,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":2000,"totalValue":50000,"startDate":"05/05/2018","endDate":"14/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project5.jpg"},{"Id":6,"title":"Children Funding","shortDescription":"This foundation will help poor children","collectedValue":1200,"totalValue":40000,"startDate":"25/05/2018","endDate":"11/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project6.jpg"},{"Id":7,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":1000,"totalValue":100000,"startDate":"22/05/2018","endDate":"22/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project7.jpg"},{"Id":8,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":500,"totalValue":50000,"startDate":"29/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project8.jpg"},{"Id":9,"title":"Rotary Club Funding","shortDescription":"This foundation will go to rotary club","collectedValue":200,"totalValue":30000,"startDate":"23/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project9.jpg"},{"Id":10,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":750,"totalValue":20000,"startDate":"05/05/2018","endDate":"08/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project10.jpg"}]}
 */

private int Status;
private String Message;
private DataBean data;

public int getStatus() {
    return Status;
}

public void setStatus(int status) {
    Status = status;
}

public String getMessage() {
    return Message;
}

public void setMessage(String message) {
    Message = message;
}

public DataBean getData() {
    return data;
}

public void setData(DataBean data) {
    this.data = data;
}

public static class DataBean {
    /**
     * TotalRecords : 10
     * Records : [{"Id":1,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":500,"totalValue":5000,"startDate":"05/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project1.jpg"},{"Id":2,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":200,"totalValue":10000,"startDate":"10/05/2018","endDate":"11/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project2.jpg"},{"Id":3,"title":"Children Funding","shortDescription":"This foundation will bring smile on there faces","collectedValue":440,"totalValue":4000,"startDate":"25/05/2018","endDate":"15/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project3.jpg"},{"Id":4,"title":"Old Age Home Funding","shortDescription":"This foundation will help old age people","collectedValue":500,"totalValue":10000,"startDate":"23/05/2018","endDate":"13/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project4.jpg"},{"Id":5,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":2000,"totalValue":50000,"startDate":"05/05/2018","endDate":"14/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project5.jpg"},{"Id":6,"title":"Children Funding","shortDescription":"This foundation will help poor children","collectedValue":1200,"totalValue":40000,"startDate":"25/05/2018","endDate":"11/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project6.jpg"},{"Id":7,"title":"Smile Crowdfunding","shortDescription":"This foundation will bring smile on there faces","collectedValue":1000,"totalValue":100000,"startDate":"22/05/2018","endDate":"22/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project7.jpg"},{"Id":8,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":500,"totalValue":50000,"startDate":"29/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project8.jpg"},{"Id":9,"title":"Rotary Club Funding","shortDescription":"This foundation will go to rotary club","collectedValue":200,"totalValue":30000,"startDate":"23/05/2018","endDate":"10/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project9.jpg"},{"Id":10,"title":"Animal Funding","shortDescription":"This foundation will help animals","collectedValue":750,"totalValue":20000,"startDate":"05/05/2018","endDate":"08/06/2018","mainImageURL":"https://testffc.nimapinfotech.com/testdatajson/project10.jpg"}]
     */

    private int TotalRecords;
    private List<RecordsBean> Records;

    public int getTotalRecords() {
        return TotalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        TotalRecords = totalRecords;
    }

    public List<RecordsBean> getRecords() {
        return Records;
    }

    public void setRecords(List<RecordsBean> records) {
        Records = records;
    }

    public static class RecordsBean {
        /**
         * Id : 1
         * title : Smile Crowdfunding
         * shortDescription : This foundation will bring smile on there faces
         * collectedValue : 500
         * totalValue : 5000
         * startDate : 05/05/2018
         * endDate : 10/06/2018
         * mainImageURL : https://testffc.nimapinfotech.com/testdatajson/project1.jpg
         */

        private int Id;

        public int getId() {
            return Id;
        }

        public void setId(int id) {
            Id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getShortDescription() {
            return shortDescription;
        }

        public void setShortDescription(String shortDescription) {
            this.shortDescription = shortDescription;
        }

        public int getCollectedValue() {
            return collectedValue;
        }

        public void setCollectedValue(int collectedValue) {
            this.collectedValue = collectedValue;
        }

        public int getTotalValue() {
            return totalValue;
        }

        public void setTotalValue(int totalValue) {
            this.totalValue = totalValue;
        }

        public String getStartDate() {
            return startDate;
        }

        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }

        public String getEndDate() {
            return endDate;
        }

        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }

        public String getMainImageURL() {
            return mainImageURL;
        }

        public void setMainImageURL(String mainImageURL) {
            this.mainImageURL = mainImageURL;
        }

        private String title;
        private String shortDescription;
        private int collectedValue;
        private int totalValue;
        private String startDate;
        private String endDate;
        private String mainImageURL;
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM