首页 > 解决方案 > 如何通过改造从 json 数组中获取嵌套对象

问题描述

我看到很多关于此的帖子,但似乎都没有工作,因为它从未指定我应该如何实际获取 enque 方法中的数据。

这是我的主要片段:

 Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://apidojo-yahoo-finance-v1.p.rapidapi.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

这是我的 pojo 课:

@SerializedName("longName")
@Expose
private String longName;

@SerializedName("symbol")
@Expose
private String symbol;

@Expose
@SerializedName("regularMarketPrice")
private int regularMarketPrice;


public String getLongName() {
    return longName;
}

public String getSymbol() {
    return symbol;
}

public int getRegularMarketPrice() {
    return regularMarketPrice;
}

这是界面:

@GET("market/get-quotes?region=US&lang=en&symbols=MSFT")
Call<StockItem> getStocks(
        @Header("x-rapidapi-host") String host,
        @Header("x-rapidapi-key") String key

);

最后是来自网站的 Json 响应:

{1 item
  "quoteResponse":{2 items
        "result":[1 item
           0:{...}97 items
           ]
"error":NULL

} }

0里面只是我想得到的股票名称符号和价格

我真的需要为我必须经历的每个 json 数组对象创建新的 java 类作为对象吗?

标签: javaapiandroid-studioretrofit

解决方案


推荐阅读