用Gson反序列化unknow json

时间:2018-09-12 12:58:56

标签: java android gson

我有一个免费的api,用于使用json进行货币跟踪: api.coinmarketcap 我需要使用Gson库将该json反序列化为我的复合Java对象。这是我的模型对象:

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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我创建货币反序列化器:

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

简单而又工作