代码之家  ›  专栏  ›  技术社区  ›  Ayusch

映射使用RxJava和改型获得的嵌套JSON响应

  •  0
  • Ayusch  · 技术社区  · 6 年前

    我从RxJava和改装开始,想创建一个简单的应用程序来显示100种加密货币的列表。

    我正在进行一个api调用,其结果如下:

    {
        "Response": "Success",
        "Message": "Coin list succesfully returned!",
        "BaseImageUrl": "https://www.cryptocompare.com",
        "BaseLinkUrl": "https://www.cryptocompare.com",
        "Data": {
            "LTC": {
                "Id": "3808",
                "Url": "/coins/ltc/overview",
                "ImageUrl": "/media/19782/ltc.png",
                "Name": "LTC",
                "CoinName": "Litecoin",
                "FullName": "Litecoin (LTC)",
                "Algorithm": "Scrypt",
                "ProofType": "PoW",
                "SortOrder": "2"
            }
            ...
        },
        "Type": 100
    }
    

    但我想要的只是“回应”和“数据”。由于为每种硬币创建100个不同的模型类是不现实的,所以我想将所有硬币的信息存储在一个名为coin的公共类中,该类看起来如下:

    public class Coins {
    
        @SerializedName("Algorithm")
        private String mAlgorithm;
        @SerializedName("CoinName")
        private String mCoinName;
        @SerializedName("FullName")
        private String mFullName;
        @SerializedName("FullyPremined")
        private String mFullyPremined;
        @SerializedName("Id")
        private String mId;
        @SerializedName("ImageUrl")
        private String mImageUrl;
        @SerializedName("Name")
        private String mName;
        @SerializedName("PreMinedValue")
        private String mPreMinedValue;
        @SerializedName("ProofType")
        private String mProofType;
        @SerializedName("SortOrder")
        private String mSortOrder;
        @SerializedName("Sponsored")
        private Boolean mSponsored;
        @SerializedName("Symbol")
        private String mSymbol;
        @SerializedName("TotalCoinSupply")
        private String mTotalCoinSupply;
        @SerializedName("TotalCoinsFreeFloat")
        private String mTotalCoinsFreeFloat;
        @SerializedName("Url")
        private String mUrl;
    
        public String getAlgorithm() {
            return mAlgorithm;
        }
    
        public void setAlgorithm(String Algorithm) {
            mAlgorithm = Algorithm;
        }
    
        public String getCoinName() {
            return mCoinName;
        }
    
        public void setCoinName(String CoinName) {
            mCoinName = CoinName;
        }
    
        public String getFullName() {
            return mFullName;
        }
    
        public void setFullName(String FullName) {
            mFullName = FullName;
        }
    
        public String getFullyPremined() {
            return mFullyPremined;
        }
    
        public void setFullyPremined(String FullyPremined) {
            mFullyPremined = FullyPremined;
        }
    
        public String getId() {
            return mId;
        }
    
        public void setId(String Id) {
            mId = Id;
        }
    
        public String getImageUrl() {
            return mImageUrl;
        }
    
        public void setImageUrl(String ImageUrl) {
            mImageUrl = ImageUrl;
        }
    
        public String getName() {
            return mName;
        }
    
        public void setName(String Name) {
            mName = Name;
        }
    
        public String getPreMinedValue() {
            return mPreMinedValue;
        }
    
        public void setPreMinedValue(String PreMinedValue) {
            mPreMinedValue = PreMinedValue;
        }
    
        public String getProofType() {
            return mProofType;
        }
    
        public void setProofType(String ProofType) {
            mProofType = ProofType;
        }
    
        public String getSortOrder() {
            return mSortOrder;
        }
    
        public void setSortOrder(String SortOrder) {
            mSortOrder = SortOrder;
        }
    
        public Boolean getSponsored() {
            return mSponsored;
        }
    
        public void setSponsored(Boolean Sponsored) {
            mSponsored = Sponsored;
        }
    
        public String getSymbol() {
            return mSymbol;
        }
    
        public void setSymbol(String Symbol) {
            mSymbol = Symbol;
        }
    
        public String getTotalCoinSupply() {
            return mTotalCoinSupply;
        }
    
        public void setTotalCoinSupply(String TotalCoinSupply) {
            mTotalCoinSupply = TotalCoinSupply;
        }
    
        public String getTotalCoinsFreeFloat() {
            return mTotalCoinsFreeFloat;
        }
    
        public void setTotalCoinsFreeFloat(String TotalCoinsFreeFloat) {
            mTotalCoinsFreeFloat = TotalCoinsFreeFloat;
        }
    
        public String getUrl() {
            return mUrl;
        }
    
        public void setUrl(String Url) {
            mUrl = Url;
        }
    
    }
    

    最后,我的映射响应类如下所示:

    public class CoinsListResponse {
        private boolean success;
        private List<Coins> coinsList;
    
        public boolean isSuccess() {
            return success;
        }
    
        public void setSuccess(boolean success) {
            this.success = success;
        }
    
        public List<Coins> getCoinsList() {
            return coinsList;
        }
    
        public void setCoinsList(List<Coins> coinsList) {
            this.coinsList = coinsList;
        }
    }
    

    我没有添加@序列化符号,因为我不知道用什么键来注释它。

    我的改装服务界面有一种将结果返回到此地图的方法:

    public interface CoinService {
    
        @NonNull
        @POST
        Observable<CoinsListResponse> getCoinList();
    
    }
    

    因为我是一个具有改装和RxAndroid的初学者,所以可能有更好的方法来做到这一点,但我不知道。如果是这样的话,也请提一下!!几天来,我一直在想这个问题,但也找不到任何答案。

    请帮忙!!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ben P.    6 年前

    改变

    private List<Coins> coinsList;
    

    @SerializedName("Data")
    private Map<String, Coins> coinsByName;
    

    你可以使用 coinsByName.values() 或致电,例如。 coinsByName.get("LTC")