0

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?

1
  • 1
    add line "wisataList = new ArrayList<>(Arrays.asList(jsonResponse.getData().getJSONObject(i)));" in your if condition inside for loop and remove before loop. So you will get only that data which match condition. Commented Jul 22, 2021 at 5:50

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use org.json.simple library. You would do it like this:

import org.json.simple.*;

// I imagine response.body() is a String, if not, convert it to String
JSONObject jsonObject = (JSONObject) JSONValue.parse(response.body());
JSONArray data = (JSONArray) jsonObject.get("data");

for (int i = 0; i < data.size(); i++) {
    JSONObject jsonObject2 = (JSONObject)data.get(i);
    String id = (String) jsonObject2.get("id");
    
    if((id != null) && (id.equals("3"))){
        String nama_ = (String) jsonObject3.get("nama");
        //...
    }
   
}

Comments

0

But.. where are you assigning the json data into?
to simplify it - a small example - hope this helps

dataset:

let jsonData = {
    "success": true,
    "data": [
        {
            "id": 1,
            "name": "test",
        },
        {
            "id": 2,
            "name": "test2",
        },
        {
            "id": 3,
            "name": "test3",
        }
    ]
};

Get id=='2':

let response = jsonData.data.find(ele => ele.id == '2');

Response now has value of:

{ "id": 2, "name": "test2" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.