如何将复杂的JSON响应放入数组列表?"on response"中写什么才能仅从 json 响应中获取记录?



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

主要活动

我正在为运行成功干杯,但现在该在响应中将数据存储在阵列中时写些什么。。。因为我得到了完整的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();
}
});
}
}

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

}

相关内容

  • 没有找到相关文章

最新更新