如何使用改型获取多个json数据(而非jsonarray)

时间:2019-04-29 10:37:42

标签: java android

如果API的返回值是jsonarray,那么我可以轻松地使用arraylist来存储和使用该值。但是,如果API返回值仅包含多个对象,我不知道如何存储该值。

//if return values are array
public interface StockApi {

    @GET("stock/{symbol}/financials")
    Observable<Financials> getFinancial(@Path("symbol") String symbol);
}

public class Financials {

    @Expose
    public String symbol;

    @Expose
    public List<Financial> financials;

}

//

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class StockService {
private static StockService instance;
private static String BASE_URL = "https://api.iextrading.com/1.0/";
private Retrofit retrofitInstance;
private StockApi stockApi;

public StockService() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new 
OkHttpClient.Builder().addInterceptor(interceptor).build();

    retrofitInstance = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

    stockApi = retrofitInstance.create(StockApi.class);
}

public StockApi getStockApi() {
    return stockApi;
}

public static StockService getInstance() {
    if (instance == null) {
        instance = new StockService();
    }
    return instance;
}
}

但下面的返回值仅由Object组成。

{"AAPL":{"quote":{"symbol":"AAPL","companyName":"Apple, Inc.","calculationPrice":"close","open":null,"openTime":null,"close":204.3,"closeTime":1556308800303,"high":205,"low":202.12,"latestPrice":204.3,"latestSource":"Close","latestTime":"April 26, 2019","latestUpdate":1556308800303,"latestVolume":18611784,"iexRealtimePrice":null,"iexRealtimeSize":null,"iexLastUpdated":null,"delayedPrice":204.3,"delayedPriceTime":1556308800303,"extendedPrice":204.48,"extendedChange":0.18,"extendedChangePercent":0.00088,"extendedPriceTime":1556528348053,"previousClose":205.28,"change":-0.98,"changePercent":-0.00477,"iexMarketPercent":null,"iexVolume":null,"avgTotalVolume":27553433,"iexBidPrice":null,"iexBidSize":null,"iexAskPrice":null,"iexAskSize":null,"marketCap":963331704000,"peRatio":16.65,"week52High":233.47,"week52Low":142,"ytdChange":0.288923}},"FB":{"quote":{"symbol":"FB","companyName":"Facebook, Inc.","calculationPrice":"close","open":null,"openTime":null,"close":191.49,"closeTime":1556308800449,"high":192.9,"low":189.09,"latestPrice":191.49,"latestSource":"Close","latestTime":"April 26, 2019","latestUpdate":1556308800449,"latestVolume":22095976,"iexRealtimePrice":null,"iexRealtimeSize":null,"iexLastUpdated":null,"delayedPrice":191.49,"delayedPriceTime":1556308800450,"extendedPrice":191.79,"extendedChange":0.3,"extendedChangePercent":0.00157,"extendedPriceTime":1556528348076,"previousClose":193.26,"change":-1.77,"changePercent":-0.00916,"iexMarketPercent":null,"iexVolume":null,"avgTotalVolume":18498923,"iexBidPrice":null,"iexBidSize":null,"iexAskPrice":null,"iexAskSize":null,"marketCap":546615864600,"peRatio":28.22,"week52High":218.62,"week52Low":123.02,"ytdChange":0.402175}},"MSFT":{"quote":{"symbol":"MSFT","companyName":"Microsoft Corp.","calculationPrice":"close","open":null,"openTime":null,"close":129.89,"closeTime":1556308800335,"high":130.515,"low":129.02,"latestPrice":129.89,"latestSource":"Close","latestTime":"April 26, 2019","latestUpdate":1556308800335,"latestVolume":23306165,"iexRealtimePrice":null,"iexRealtimeSize":null,"iexLastUpdated":null,"delayedPrice":129.89,"delayedPriceTime":1556308800335,"extendedPrice":130.45,"extendedChange":0.56,"extendedChangePercent":0.00431,"extendedPriceTime":1556528690432,"previousClose":129.15,"change":0.74,"changePercent":0.00573,"iexMarketPercent":null,"iexVolume":null,"avgTotalVolume":24243972,"iexBidPrice":null,"iexBidSize":null,"iexAskPrice":null,"iexAskSize":null,"marketCap":995323689800,"peRatio":28.61,"week52High":131.37,"week52Low":92.45,"ytdChange":0.29024300000000003}}}

0 个答案:

没有答案