Robby Ramadhan
Robby Ramadhan

Reputation: 31

How to get JSON Array without object key using retrofit

I'm having trouble loading want to retrieve url data from JSON below

{
     "status": "success",
     "message": "This is a message",
     "item":{
         "id":"1",
         "video":{
             "url" : [
                 "https://url1.com",
                 "https://url1.com",
                 "https://url1.com"
             ]
          }
     }
}

ResponseData

public class ResponseData {

    @SerializedName("status")
    private String status;
    @SerializedName("message")
    private String message;
    @SerializedName("item")
    private Item item;

    //Getters
}

Item

public class Item {
   
   @SerializedName("id")
   private String id;
   @SerializedName("video")
   private Video video;
   
   //Getters
}

Video

public class Video { 

   @SerializedName("url")
   private List<String> urlList;
   
   //Getters
}

What should I do after this to get each URL and apply it in Retrofit onResponse?

 @Override
 public void onResponse(@NonNull Call<ResponseData> call, @NonNull Response<ResponseData> response) {
     if (response.isSuccessful()) {
         ResponseData resp = response.body();

         //For the call I want results like the method below
         DataFromServer d = new DataFromServer();
         d.url = resp.getItem().getVideo().getUrlList().getUrl1(); // I want it like this
     }
 }

Upvotes: 1

Views: 571

Answers (1)

prudhvir3ddy
prudhvir3ddy

Reputation: 1036

    resp.getItem().getVideo().getUrlList().forEach((url) -> {
       Log.i("url", url) // you can get each url here
     }
    );

you are basically getting a list from server.

you don't need keys to access them. you can store them in list and also do

list.get(0) - for first url

list.get(1) - for second url

and so on.

Upvotes: 1

Related Questions