【问题标题】:How to get Complex JSON response into Array List? What to write in "on response" to get only records from json response?如何将复杂的 JSON 响应放入数组列表中?在“响应”中写什么以仅从 json 响应中获取记录?
【发布时间】:2021-05-14 06:58:54
【问题描述】:

如何将复杂的 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();
}

模态类:

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

我在运行时获得了成功祝酒,但现在要写入什么以响应将数据存储在数组中...因为我在响应时得到了整个 apiresponse。

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

【问题讨论】:

    标签: java android json


    【解决方案1】:

    修改 MODAL 类为:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多