-1

How to convert a jsonArray with multiple jsonObjects to an arraylist using Gson.

Here is my Json String:

[{"AccommoAddress":{  

     "PostalCode":2109,
     "Street":"22 Ararat Str",
     "Town":"Westdene"
  },
  "AccommoDetails":{  
     "AccommoId":0,
     "CleaningService":1,
     "DSTV":1,
     "DedicatedStudyArea":1

  },
  "AccommoID":1,
  "AccommoMainImage":{  
     "CategoryId":0,
     "ContentType":".png",
     "DateUploaded":"2016-07-16",
     "FileSize":2362,
     "ImageCategory":"MAIN",
     "ImageId":1,
     "ImageName":"images.png"
  }]
0

1 Answer 1

0

First , it's not a correct Json format.

"ImageName":"images.png" <- (you need to put a "}" here )

Second , create a entity

public class TargetEntity {

    private AccommoAddressEntity AccommoAddress;
    private AccommoDetailsEntity AccommoDetails;
    private int AccommoID;
    private AccommoMainImageEntity AccommoMainImage;

    public AccommoAddressEntity getAccommoAddress() {
        return AccommoAddress;
    }

    public void setAccommoAddress(AccommoAddressEntity AccommoAddress) {
        this.AccommoAddress = AccommoAddress;
    }

    public AccommoDetailsEntity getAccommoDetails() {
        return AccommoDetails;
    }

    public void setAccommoDetails(AccommoDetailsEntity AccommoDetails) {
        this.AccommoDetails = AccommoDetails;
    }

    public int getAccommoID() {
        return AccommoID;
    }

    public void setAccommoID(int AccommoID) {
        this.AccommoID = AccommoID;
    }

    public AccommoMainImageEntity getAccommoMainImage() {
        return AccommoMainImage;
    }

    public void setAccommoMainImage(AccommoMainImageEntity AccommoMainImage) {
        this.AccommoMainImage = AccommoMainImage;
    }

    public static class AccommoAddressEntity {
        private int PostalCode;
        private String Street;
        private String Town;

        public int getPostalCode() {
            return PostalCode;
        }

        public void setPostalCode(int PostalCode) {
            this.PostalCode = PostalCode;
        }

        public String getStreet() {
            return Street;
        }

        public void setStreet(String Street) {
            this.Street = Street;
        }

        public String getTown() {
            return Town;
        }

        public void setTown(String Town) {
            this.Town = Town;
        }
    }

    public static class AccommoDetailsEntity {
        private int AccommoId;
        private int CleaningService;
        private int DSTV;
        private int DedicatedStudyArea;

        public int getAccommoId() {
            return AccommoId;
        }

        public void setAccommoId(int AccommoId) {
            this.AccommoId = AccommoId;
        }

        public int getCleaningService() {
            return CleaningService;
        }

        public void setCleaningService(int CleaningService) {
            this.CleaningService = CleaningService;
        }

        public int getDSTV() {
            return DSTV;
        }

        public void setDSTV(int DSTV) {
            this.DSTV = DSTV;
        }

        public int getDedicatedStudyArea() {
            return DedicatedStudyArea;
        }

        public void setDedicatedStudyArea(int DedicatedStudyArea) {
            this.DedicatedStudyArea = DedicatedStudyArea;
        }
    }

    public static class AccommoMainImageEntity {
        private int CategoryId;
        private String ContentType;
        private String DateUploaded;
        private int FileSize;
        private String ImageCategory;
        private int ImageId;
        private String ImageName;

        public int getCategoryId() {
            return CategoryId;
        }

        public void setCategoryId(int CategoryId) {
            this.CategoryId = CategoryId;
        }

        public String getContentType() {
            return ContentType;
        }

        public void setContentType(String ContentType) {
            this.ContentType = ContentType;
        }

        public String getDateUploaded() {
            return DateUploaded;
        }

        public void setDateUploaded(String DateUploaded) {
            this.DateUploaded = DateUploaded;
        }

        public int getFileSize() {
            return FileSize;
        }

        public void setFileSize(int FileSize) {
            this.FileSize = FileSize;
        }

        public String getImageCategory() {
            return ImageCategory;
        }

        public void setImageCategory(String ImageCategory) {
            this.ImageCategory = ImageCategory;
        }

        public int getImageId() {
            return ImageId;
        }

        public void setImageId(int ImageId) {
            this.ImageId = ImageId;
        }

        public String getImageName() {
            return ImageName;
        }

        public void setImageName(String ImageName) {
            this.ImageName = ImageName;
        }
    }
}

Third

Gson gson  = new Gson();

TargetEntity[]  target  = gson.fromJson(JsonString,TargetEntity[].class)
Sign up to request clarification or add additional context in comments.

3 Comments

Is it works ? if so , make me the correct answer
It works but for AccommoMainImage I'm receiving a null pointer exception
Thank you very much it works I figured out whats causing the null pointer exception, Am using glide on the recyclerview so every time i scroll down the images load but when i scroll up again it causes the null pointer exception and thank you again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.