簡體   English   中英

如何將復雜的 JSON 響應放入數組列表? 在“響應”中寫什么以僅從 json 響應中獲取記錄?

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

如何將復雜的 JSON 響應放入數組列表?

我只想顯示 json 以下的記錄,但由於我是新手,所以我不知道如何將此類 json 數據存儲到數組列表中,請指導我解決以下問題,因為我搜索了許多答案但無法使其工作。

JSON 響應

    {
"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接口

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

模態 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;
        }

    }

}

主要活動

我在運行時得到了成功祝酒,但現在要寫什么來響應將數據存儲在數組中......因為我得到了整個響應。

    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();
            }
        });
    }
}

將 MODAL class 修改為:

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;
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM