猿问

使用 Gson 反序列化未知 json

我有一个免费的 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;

    }

}

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


胡子哥哥
浏览 200回答 2
2回答

绝地无双

您可以在此处使用输入链接描述从 json 创建您的 pojo 类

至尊宝的传说

我创建货币反序列化器:public class CurrencyDeserializer implements JsonDeserializer<Currency> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Currency deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {&nbsp; &nbsp; &nbsp; &nbsp; Currency currency = new Currency();&nbsp; &nbsp; &nbsp; &nbsp; JsonObject currencyObject = json.getAsJsonObject();&nbsp; &nbsp; &nbsp; &nbsp; JsonElement id = currencyObject.get("id");&nbsp; &nbsp; &nbsp; &nbsp; if(!id.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setId(id.getAsInt());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement name = currencyObject.get("name");&nbsp; &nbsp; &nbsp; &nbsp; if(!name.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setName(name.getAsString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement symbol = currencyObject.get("symbol");&nbsp; &nbsp; &nbsp; &nbsp; if(!symbol.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setSymbol(symbol.getAsString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement slug = currencyObject.get("website_slug");&nbsp; &nbsp; &nbsp; &nbsp; if(!slug.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setWebsiteSlug(slug.getAsString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement rank = currencyObject.get("rank");&nbsp; &nbsp; &nbsp; &nbsp; if(!rank.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setRank(rank.getAsLong());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement circulatingSupply = currencyObject.get("circulating_supply");&nbsp; &nbsp; &nbsp; &nbsp; if(!circulatingSupply.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setCirculatingSupply(circulatingSupply.getAsLong());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement totalSupply = currencyObject.get("total_supply");&nbsp; &nbsp; &nbsp; &nbsp; if(!totalSupply.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setTotalSupply(totalSupply.getAsLong());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement maxSupply = currencyObject.get("max_supply");&nbsp; &nbsp; &nbsp; &nbsp; if(!maxSupply.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setMaxSupply(maxSupply.getAsLong());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonElement lastUpdated = currencyObject.get("last_updated");&nbsp; &nbsp; &nbsp; &nbsp; if(!lastUpdated.isJsonNull()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long l = lastUpdated.getAsLong() * 1000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.setLastUpdated(new Date(l));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JsonObject quotes = currencyObject.get("quotes").getAsJsonObject();&nbsp; &nbsp; &nbsp; &nbsp; for(Map.Entry<String, JsonElement> rootObj : quotes.entrySet())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quote quote = context.deserialize(rootObj.getValue(), Quote.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quote.setName(rootObj.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currency.addQuote(quote);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return currency;&nbsp; &nbsp; }}简单和工作
随时随地看视频慕课网APP

相关分类

Java
我要回答