英文:
Retrieved data from JSON but can't Accessing Arraylist from JSONobject
问题
以下是您要翻译的内容:
我在Android和Java编程方面非常新,并且正在开发我的技能。
现在,我在访问JSON响应中的ArrayList时遇到了一个问题。
问题在于我不知道如何根据JSON嵌套对象来初始化我的变量,并且在不将其设置为RecyclerView等情况下访问它。
以下是JSON数据
```json
{"kode":1,
"pesan":"Anda Berhasil Login",
"data":[{"Id_Ortu":"1","NamaLengkapOrtu":"Alpiyan Yamin"}]}
ResponseModel
类
public class ResponseModel {
private int kode;
private String pesan,sql;
private List<DataModel> data;
// 其他方法...
}
DataModel
类
public class DataModel {
private int Id_Perizinan, Id_Ortu;
private String NamaLengkapSantri, NamaLengkapOrtu, Alasan_Izin, Tgl_Pengajuan,
Durasi_Izin, Tgl_Kembali;
// 其他方法...
}
以下是我尝试检索数据的方式
public void loginAplikasi(){
APIRequestData apiRequestData = RetroServer.koneksiRetrofit().create(APIRequestData.class);
Call<ResponseModel> loginApp = apiRequestData.ardLogin(username,password);
loginApp.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
int kode = response.body().getKode();
String pesan = response.body().getPesan();
listData = response.body().getData();
int Id_Ortu = 0;
String NamaLengkapOrtu;
// 在这里处理数据...
}
@Override
public void onFailure(Call<ResponseModel> call, Throwable t) {
alertDialog(t.getMessage());
}
});
}
我成功地从JSON中提取了“kode”和“pesan”,使用了以下代码
int kode = response.body().getKode();
String pesan = response.body().getPesan();
我已经声明了一个名为listData的新ArrayList
List<DataModel> listData = new ArrayList<>();
然而,我如何访问包含在data中的array的值
listData = response.body().getData();
以便我可以初始化变量(NamaLengkapOrtu = listdata(arrayIndex))
提前致谢。
<details>
<summary>英文:</summary>
I'm very new in Android and Java Programming and currently developing my skill.
now, i faced a problem with accessing Arraylist from JSONresponse.
It's just i don't know how to initiate my Variable based on Json nested object, and access it without setting it to recylerview etc.
so this is the JSON
{"kode":1,
"pesan":"Anda Berhasil Login",
"data":[{"Id_Ortu":"1","NamaLengkapOrtu":"Alpiyan
Yamin"}]}
`ResponseModel` class
public class ResponseModel {
private int kode;
private String pesan,sql;
private List<DataModel> data;
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public int getKode() {
return kode;
}
public void setKode(int kode) {
this.kode = kode;
}
public String getPesan() {
return pesan;
}
public void setPesan(String pesan) {
this.pesan = pesan;
}
public List<DataModel> getData() {
return data;
}
public void setData(List<DataModel> data) {
this.data = data;
}
}
`ResponseModel` class
public class DataModel {
private int Id_Perizinan, Id_Ortu;
private String NamaLengkapSantri, NamaLengkapOrtu, Alasan_Izin, Tgl_Pengajuan,
Durasi_Izin, Tgl_Kembali;
public int getId_Perizinan() {
return Id_Perizinan;
}
public void setId_Perizinan(int id_Perizinan) {
Id_Perizinan = id_Perizinan;
}
public int getId_Ortu() {
return Id_Ortu;
}
public void setId_Ortu(int id_Ortu) {
Id_Ortu = id_Ortu;
}
public String getNamaLengkapSantri() {
return NamaLengkapSantri;
}
public void setNamaLengkapSantri(String namaLengkapSantri) {
NamaLengkapSantri = namaLengkapSantri;
}
public String getNamaLengkapOrtu() {
return NamaLengkapOrtu;
}
public void setNamaLengkapOrtu(String namaLengkapOrtu) {
NamaLengkapOrtu = namaLengkapOrtu;
}
public String getAlasan_Izin() {
return Alasan_Izin;
}
public void setAlasan_Izin(String alasan_Izin) {
Alasan_Izin = alasan_Izin;
}
public String getTgl_Pengajuan() {
return Tgl_Pengajuan;
}
public void setTgl_Pengajuan(String tgl_Pengajuan) {
Tgl_Pengajuan = tgl_Pengajuan;
}
public String getDurasi_Izin() {
return Durasi_Izin;
}
public void setDurasi_Izin(String durasi_Izin) {
Durasi_Izin = durasi_Izin;
}
public String getTgl_Kembali() {
return Tgl_Kembali;
}
public void setTgl_Kembali(String tgl_Kembali) {
Tgl_Kembali = tgl_Kembali;
}
and this is how I try to retrieve the data
public void loginAplikasi(){
APIRequestData apiRequestData = RetroServer.koneksiRetrofit().create(APIRequestData.class);
Call<ResponseModel> loginApp = apiRequestData.ardLogin(username,password);
loginApp.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
int kode = response.body().getKode();
String pesan = response.body().getPesan();
listData = response.body().getData();
int Id_Ortu = 0;
String NamaLengkapOrtu;
alertDialog(NamaLengkapOrtu);
} else {
alertDialog(pesan);
}
}
@Override
public void onFailure(Call<ResponseModel> call, Throwable t) {
alertDialog(t.getMessage());
}
});
}
i successfully catched 'kode' and 'pesan' From JSON by using
int kode = response.body().getKode();
String pesan = response.body().getPesan();
i've declared a listData as new arraylist
List<DataModel> listData = new ArrayList<>();
however, how can i access the value of
listData = response.body().getData();
which contained *array* inside of **data**
so i can initiate variable ( NamaLengkapOrtu = listdata(arrayIndex) )
Thanks in advance.
</details>
# 答案1
**得分**: 1
关于 `DataModel` 类的问题。
```java
public class DataModel {
@SerializedName("Id_Ortu")
@Expose
private String idOrtu;
@SerializedName("NamaLengkapOrtu")
@Expose
private String namaLengkapOrtu;
public String getIdOrtu() {
return idOrtu;
}
public void setIdOrtu(String idOrtu) {
this.idOrtu = idOrtu;
}
public String getNamaLengkapOrtu() {
return namaLengkapOrtu;
}
public void setNamaLengkapOrtu(String namaLengkapOrtu) {
this.namaLengkapOrtu = namaLengkapOrtu;
}
}
然后可以按以下方式访问数据:
String namaLengkapOrtu = listdata.get(index).getNamaLengkapOrtu();
String idOrtu = listdata.get(index).getIdOrtu();
英文:
The issue with DataModel
class.
public class DataModel {
@SerializedName("Id_Ortu")
@Expose
private String idOrtu;
@SerializedName("NamaLengkapOrtu")
@Expose
private String namaLengkapOrtu;
public String getIdOrtu() {
return idOrtu;
}
public void setIdOrtu(String idOrtu) {
this.idOrtu = idOrtu;
}
public String getNamaLengkapOrtu() {
return namaLengkapOrtu;
}
public void setNamaLengkapOrtu(String namaLengkapOrtu) {
this.namaLengkapOrtu = namaLengkapOrtu;
}
}
Then you can access data as follows:
String namaLengkapOrtu = listdata.get(index).getNamaLengkapOrtu();
String idOrtu = listdata.get(index).getIdOrtu();
答案2
得分: 0
以下是翻译好的内容:
{
"kode":1,
"pesan":"您已成功登录",
"data":[
{
"Id_Ortu":"1",
"NamaLengkapOrtu":"Alpiyan Yamin"
}
]
}
在您的模型类中使用以下注解:
@SerializedName("data")
@Expose
在这里,"data" 是您想要从 JSON 中获取的键。
尝试使用这个 ResponseModel
类:
public class ResponseModel {
private int kode;
private String pesan, SQL;
@SerializedName("data")
@Expose
private List<DataModel> data;
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public int getKode() {
return kode;
}
public void setKode(int kode) {
this.kode = kode;
}
public String getPesan() {
return pesan;
}
public void setPesan(String pesan) {
this.pesan = pesan;
}
public List<DataModel> getData() {
return data;
}
public void setData(List<DataModel> data) {
this.data = data;
}
}
英文:
As your JSON response is:
{"kode":1,
"pesan":"Anda Berhasil Login",
"data":[{"Id_Ortu":"1","NamaLengkapOrtu":"Alpiyan
Yamin"}]}
Use the below annotation in your model class:
@SerializedName("data")
@Expose
Here, "data" is the key you want to get from JSON.
Try to use this ResponseModel
class:
public class ResponseModel {
private int kode;
private String pesan,SQL;
@SerializedName("data")
@Expose
private List<DataModel> data;
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public int getKode() {
return kode;
}
public void setKode(int kode) {
this.kode = kode;
}
public String getPesan() {
return pesan;
}
public void setPesan(String pesan) {
this.pesan = pesan;
}
public List<DataModel> getData() {
return data;
}
public void setData(List<DataModel> data) {
this.data = data;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论