Home > Blockchain >  How to parse json array datas and save it as ArrayList?
How to parse json array datas and save it as ArrayList?

Time:09-03

if (response.isSuccessful()) {
    dataList.addAll(/* HERE */);
}

Response

public class WishAllResponse {

    WishResponseList wishResponseList;

    public class WishResponseList {
        int id;
        String title;
        String contents;
        String writer;
        boolean clear;
        String color;

        public int getId() {
            return id;
        }

        public String getTitle() {
            return title;
        }

        public String getContents() {
            return contents;
        }

        public String getWriter() {
            return writer;
        }

        public boolean isClear() {
            return clear;
        }

        public String getColor() {
            return color;
        }
    }
}

API : "wishAllResponse" : [ { "key1" : "value1", "key2" : "value2" }, {"key3" : "value3", "key4" : "value4" } ]

I want to get the json object, which key has array as value. In normal case, when json file has only json array, I used response.body() to get value as arraylist. But response body has changed.

CodePudding user response:

Try this -

Modify model class -

public class WishAllResponse {
        int id;
        String title;
        String contents;
        String writer;
        boolean clear;
        String color;

        public int getId() {
            return id;
        }

        public String getTitle() {
            return title;
        }

        public String getContents() {
            return contents;
        }

        public String getWriter() {
            return writer;
        }

        public boolean isClear() {
            return clear;
        }

        public String getColor() {
            return color;
        }

}

Modify the GET call -

@GET("/wish/all") Call<ArrayList<WishAllResponse>> getAll(@Header("Authorization") String accessToken);

Parse response like this -

ArrayList<WishAllResponse> list =  response.body();
dataList.addAll(list);
  • Related