JsonArray使用不同的jsonObjects To ArrayList android使用gson

时间:2016-09-05 00:06:05

标签: java android json arraylist

如何使用jsonArrayjsonObjects多个arraylist转换为Gson

这是我的Json字符串:

[{"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"
  }]

1 个答案:

答案 0 :(得分:0)

首先,它不是正确的Json格式。

" ImageName":" images.png" < - (你需要放一个"}"这里)

其次,创建一个实体

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;
        }
    }
}

第三

Gson gson  = new Gson();

TargetEntity[]  target  = gson.fromJson(JsonString,TargetEntity[].class)