从JSON中检索到数据,但无法从JSON对象访问ArrayList。

huangapple go评论83阅读模式
英文:

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&#39;m very new in Android and Java Programming and currently developing my skill.

now, i faced a problem with accessing Arraylist from JSONresponse.

It&#39;s just i don&#39;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&lt;DataModel&gt; 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&lt;DataModel&gt; getData() {
            return data;
        }
    
        public void setData(List&lt;DataModel&gt; 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&lt;ResponseModel&gt; loginApp = apiRequestData.ardLogin(username,password);

    loginApp.enqueue(new Callback&lt;ResponseModel&gt;() {

        @Override
        public void onResponse(Call&lt;ResponseModel&gt; call, Response&lt;ResponseModel&gt; 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&lt;ResponseModel&gt; call, Throwable t) {
           alertDialog(t.getMessage());
        }
    });

}

i successfully catched &#39;kode&#39; and &#39;pesan&#39; From JSON by using

int kode = response.body().getKode();
String pesan = response.body().getPesan();

i&#39;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(&quot;Id_Ortu&quot;)
    @Expose
    private String idOrtu;
    @SerializedName(&quot;NamaLengkapOrtu&quot;)
    @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:

{&quot;kode&quot;:1,
&quot;pesan&quot;:&quot;Anda Berhasil Login&quot;,
&quot;data&quot;:[{&quot;Id_Ortu&quot;:&quot;1&quot;,&quot;NamaLengkapOrtu&quot;:&quot;Alpiyan
Yamin&quot;}]}

Use the below annotation in your model class:

@SerializedName(&quot;data&quot;)
@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(&quot;data&quot;)
     @Expose     
     private List&lt;DataModel&gt; 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&lt;DataModel&gt; getData() {
         return data;
     }
 
     public void setData(List&lt;DataModel&gt; data) {
         this.data = data;
     }
}

huangapple
  • 本文由 发表于 2020年10月25日 15:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64521518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定