Issue
I want to get a specific value from my nested JSON object like this:
{
"success": true,
"data": [
{
...
},
{
"id": 5,
"nama": "Ngagoes Ulin Kalembur",
"harga": 250000,
"deskripsi": "Menikmati serunya berwisata keliling desa-desa di sekitaran desa alamendah menggunakan sepeda, menyusuri pemukiman, kebun-kebun dan pemandangan anda akan menemukan hal menarik selama perjalanan di desa alamendah, Pengemasan sayuran, pengrajin sovenir dll.",
"gambarid": 11,
"paketan": true,
"fasilitas": "Sepeda,Angkutan Umum,Wisata Perternakan,Wisata Kuliner,Wisata Pertanian,Snack,Pemandu Lokal,Air Mineral",
"totalTiket": 100,
"minTiket": 50,
"createdAt": "2021-03-07T10:44:10.000Z",
"updatedAt": "2021-04-28T13:07:05.000Z",
"deletedAt": null,
"gambarId": 11,
"Gambar": {
"id": 11,
"url": "https://desaalamendah.id/images/upload/ImageWisata-1615114030139.png",
"isGallery": null,
"createdAt": "2021-03-07T10:44:10.000Z",
"updatedAt": "2021-03-07T10:47:10.000Z",
"deletedAt": null
}
},
I already try for()
and if()
condition like this:
@override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response)
{
JSONResponse jsonResponse = response.body();
wisataList = new ArrayList<>(Arrays.asList(jsonResponse.getData()));
Intent intent = getIntent();
int id = intent.getIntExtra(HomeActivity.EXTRA_NUMBER, 0);
for (int i = 0; i<jsonResponse.getData().length; i++)
{
if (id == i)
{
PutDataIntoRecyclerView(wisataList);
}
}
}
When I run it, it keeps returning all of the values. How to get a specific value, for example id = 3
, from my nested JSON object?
Solution
From the following class: JSONResponse
Not sure if you were using a library, or has a POJOs called JSONResponse
Also, you're calling the JSONResponse#getData
, it means there's a getter called getData
presumably, your project already has a POJO for the attributes inside "data": [ { ... }, { ... } ]
objects
E.g:
public class JSONResponse {
private List<Data> data;
private String success;
// getter & setter omitted
}
public class Data {
private Integer id;
private String nama;
private Integer harga;
private String deskripsi;
private Integer gambarid;
private Boolean paketan;
private String fasilitas;
private Integer totalTiket;
private Integer minTiket;
private String createdAt;
private String updatedAt;
private Object deletedAt;
private Integer gambarId;
private Gambar gambar;
// getter and setter omitted
}
public class Gambar {
private Integer id;
private String url;
private Object isGallery;
private String createdAt;
private String updatedAt;
private Object deletedAt;
// getter and setter omitted
}
Generate POJOs from JSON: https://www.jsonschema2pojo.org/
Or you can use plugins: https://plugins.jetbrains.com/plugin/8533-json2pojo
Here's the alternative approach might works for your usecases:
@override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response)
{
JSONResponse jsonResponse = response.body();
wisataList = new ArrayList<Data>();
Intent intent = getIntent();
int id = intent.getIntExtra(HomeActivity.EXTRA_NUMBER, 0);
for (int i = 0; i<jsonResponse.getData().length; i++)
{
Data wisata = jsonResponse.getData().get(i);
if (id == wisata.getId())
{
wisatList.add(wisata);
// break; // optional if you want to stop iterating, if the wisata already found
}
}
PutDataIntoRecyclerView(wisataList);
Answered By - mochadwi
Answer Checked By - Marilyn (JavaFixing Volunteer)