第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Gson 反序列化未知 json

使用 Gson 反序列化未知 json

胡子哥哥 2021-09-15 15:28:23
我有一個(gè)免費(fèi)的 api,用于使用 json 進(jìn)行貨幣跟蹤: api.coinmarketcap 我需要使用 Gson 庫將這個(gè) json 反序列化為我的復(fù)合 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 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以在此處使用輸入鏈接描述從 json 創(chuàng)建您的 pojo 類


查看完整回答
反對 回復(fù) 2021-09-15
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

我創(chuàng)建貨幣反序列化器:


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;

    }

}

簡單和工作


查看完整回答
反對 回復(fù) 2021-09-15
  • 2 回答
  • 0 關(guān)注
  • 222 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)