代码之家  ›  专栏  ›  技术社区  ›  Iliya Mashin

用Gson反序列化未知的json

  •  2
  • Iliya Mashin  · 技术社区  · 6 年前

    我得到了一个使用json进行货币跟踪的免费api: api.coinmarketcap

    public class Quote {
        @SerializedName("quotes")
        private String mName;
        @SerializedName("price")
        private double mPrice;
    
        public Quote(String name, double price) {
            mName = name;
            mPrice = price;
        }
    
        public String getName() {
            return mName;
        }
    
        public double getPrice() {
            return mPrice;
        }
    }
    

    以及:

    public class Currency {
        private int mId;
        private String mSymbol;
        private byte mRank;
        private String mWebsiteSlug;
        private int mMaxSupply;
        private Quote mQuote;
    
        public Currency(int id, String symbol, byte rank, String websiteSlug, int maxSupply) {
            mId = id;
            mSymbol = symbol;
            mRank = rank;
            mWebsiteSlug = websiteSlug;
            mMaxSupply = maxSupply;
        }
    
        public int getId() {
            return mId;
        }
    
        public String getSymbol() {
            return mSymbol;
        }
    
        public byte getRank() {
            return mRank;
        }
    
        public String getWebsiteSlug() {
            return mWebsiteSlug;
        }
    
        public int getMaxSupply() {
            return mMaxSupply;
        }
    
        public Quote getQuote() {
            return mQuote;
        }
    }
    

    我不能用这种嵌套反序列化。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Yegor Gruk    6 年前

    你可以用 enter link description here 从json创建pojo类

        2
  •  0
  •   Iliya Mashin    6 年前

    我创建货币反序列化程序:

    public class CurrencyDeserializer implements JsonDeserializer<Currency> {
        @Override
        public Currency deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    
            Currency currency = new Currency();
    
            JsonObject currencyObject = json.getAsJsonObject();
    
            JsonElement id = currencyObject.get("id");
            if(!id.isJsonNull()) {
                currency.setId(id.getAsInt());
            }
    
            JsonElement name = currencyObject.get("name");
            if(!name.isJsonNull()) {
                currency.setName(name.getAsString());
            }
    
            JsonElement symbol = currencyObject.get("symbol");
            if(!symbol.isJsonNull()) {
                currency.setSymbol(symbol.getAsString());
            }
    
            JsonElement slug = currencyObject.get("website_slug");
            if(!slug.isJsonNull()) {
                currency.setWebsiteSlug(slug.getAsString());
            }
    
            JsonElement rank = currencyObject.get("rank");
            if(!rank.isJsonNull()) {
                currency.setRank(rank.getAsLong());
            }
    
            JsonElement circulatingSupply = currencyObject.get("circulating_supply");
            if(!circulatingSupply.isJsonNull()) {
                currency.setCirculatingSupply(circulatingSupply.getAsLong());
            }
    
            JsonElement totalSupply = currencyObject.get("total_supply");
            if(!totalSupply.isJsonNull()) {
                currency.setTotalSupply(totalSupply.getAsLong());
            }
    
            JsonElement maxSupply = currencyObject.get("max_supply");
            if(!maxSupply.isJsonNull()) {
                currency.setMaxSupply(maxSupply.getAsLong());
            }
    
            JsonElement lastUpdated = currencyObject.get("last_updated");
            if(!lastUpdated.isJsonNull()) {
                Long l = lastUpdated.getAsLong() * 1000;
                currency.setLastUpdated(new Date(l));
            }
    
            JsonObject quotes = currencyObject.get("quotes").getAsJsonObject();
    
            for(Map.Entry<String, JsonElement> rootObj : quotes.entrySet())
            {
                Quote quote = context.deserialize(rootObj.getValue(), Quote.class);
                quote.setName(rootObj.getKey());
                currency.addQuote(quote);
            }
    
            return currency;
        }
    }