【问题标题】:How to work with retrofit in Android when JSON data is dynamic?当 JSON 数据是动态的时,如何在 Android 中进行改造?
【发布时间】:2019-06-23 06:13:07
【问题描述】:

我正在制作一个向用户显示每日股票数据的应用。问题是,我正在使用改造来进行 API 调用。使用这种类型的 JSON,我需要一个带有每个股票代码的 POJO。这显然是不可能的。 我希望能够做一些事情 - listOfStocks[1].getQuote().getLatestPrice();

这是我收到的 JSON 数据。

{  
   "AAPL":{  
      "quote":{  
         "symbol":"AAPL",
         "companyName":"Apple Inc.",
         "primaryExchange":"Nasdaq Global Select",
         "sector":"Technology",
         "calculationPrice":"close",
         "open":156.21,
         "openTime":1548772200635,
         "close":154.68,
         "closeTime":1548795600703,
         "high":158.13,
         "low":154.11,
         "latestPrice":154.68,
         "latestSource":"Close",
         "latestTime":"January 29, 2019",
         "latestUpdate":1548795600703,
         "latestVolume":39402866,
         "iexRealtimePrice":161.04,
         "iexRealtimeSize":90,
         "iexLastUpdated":1548799005088,
         "delayedPrice":154.68,
         "delayedPriceTime":1548795600703,
         "extendedPrice":160.6,
         "extendedChange":5.92,
         "extendedChangePercent":0.03827,
         "extendedPriceTime":1548799199389,
         "previousClose":156.3,
         "change":-1.62,
         "changePercent":-0.01036,
         "iexMarketPercent":0.02338,
         "iexVolume":921239,
         "avgTotalVolume":42308638,
         "iexBidPrice":0,
         "iexBidSize":0,
         "iexAskPrice":0,
         "iexAskSize":0,
         "marketCap":731605928040,
         "peRatio":13.03,
         "week52High":233.47,
         "week52Low":142,
         "ytdChange":-0.030876717325227843
      }
   },
   "FB":{  
      "quote":{  
         "symbol":"FB",
         "companyName":"Facebook Inc.",
         "primaryExchange":"Nasdaq Global Select",
         "sector":"Technology",
         "calculationPrice":"close",
         "open":148,
         "openTime":1548772200837,
         "close":144.19,
         "closeTime":1548795600692,
         "high":148.1,
         "low":143.43,
         "latestPrice":144.19,
         "latestSource":"Close",
         "latestTime":"January 29, 2019",
         "latestUpdate":1548795600692,
         "latestVolume":17353892,
         "iexRealtimePrice":144.25,
         "iexRealtimeSize":100,
         "iexLastUpdated":1548796485236,
         "delayedPrice":144.19,
         "delayedPriceTime":1548795600692,
         "extendedPrice":145.05,
         "extendedChange":0.86,
         "extendedChangePercent":0.00596,
         "extendedPriceTime":1548799197301,
         "previousClose":147.47,
         "change":-3.28,
         "changePercent":-0.02224,
         "iexMarketPercent":0.02505,
         "iexVolume":434715,
         "avgTotalVolume":25773532,
         "iexBidPrice":0,
         "iexBidSize":0,
         "iexAskPrice":0,
         "iexAskSize":0,
         "marketCap":414371435774,
         "peRatio":19.51,
         "week52High":218.62,
         "week52Low":123.02,
         "ytdChange":0.04048110849056598
      }
   },
   "MSFT":{  
      "quote":{  
         "symbol":"MSFT",
         "companyName":"Microsoft Corporation",
         "primaryExchange":"Nasdaq Global Select",
         "sector":"Technology",
         "calculationPrice":"close",
         "open":104.88,
         "openTime":1548772200862,
         "close":102.94,
         "closeTime":1548795600515,
         "high":104.97,
         "low":102.17,
         "latestPrice":102.94,
         "latestSource":"Close",
         "latestTime":"January 29, 2019",
         "latestUpdate":1548795600515,
         "latestVolume":31105047,
         "iexRealtimePrice":102.895,
         "iexRealtimeSize":100,
         "iexLastUpdated":1548795598662,
         "delayedPrice":102.94,
         "delayedPriceTime":1548795600515,
         "extendedPrice":103.67,
         "extendedChange":0.73,
         "extendedChangePercent":0.00709,
         "extendedPriceTime":1548799195514,
         "previousClose":105.08,
         "change":-2.14,
         "changePercent":-0.02037,
         "iexMarketPercent":0.03309,
         "iexVolume":1029266,
         "avgTotalVolume":40670438,
         "iexBidPrice":0,
         "iexBidSize":0,
         "iexAskPrice":0,
         "iexAskSize":0,
         "marketCap":790189956684,
         "peRatio":26.53,
         "week52High":116.18,
         "week52Low":83.83,
         "ytdChange":-0.002371582278481079
      }
   }
}

【问题讨论】:

  • 你能详细解释一下到底是什么问题吗?

标签: android json gson retrofit pojo


【解决方案1】:

我个人会为quotesymbol 制作一个模型,然后使用Map<String, Symbol>

这是一个示例,其中模型只有 latestPrice 属性来缩短示例:

class Quote {
     @SerializedName("latestPrice")
     private double latestPrice;
     // ...
}

class Symbol {
     @SerializedName("quote")
     private Quote quote;
      //...
}

现在使用 Map<String, Symbol> 进行改造调用应该会为您提供一个地图,您可以在其中迭代条目并获取每个条目的最新价格。

【讨论】:

  • 我会使用 'Map' 还是 'List>' 拨打电话?
  • Map<String, Symbol>' - json 返回一个对象作为根元素。 Retrofit 可以将其映射到字符串到符号的映射。
  • 如果您能看一下并提出建议,那就太好了。链接在这里。谢谢! github.com/deepanshpahwa/StockApp
  • 就是这样。它不工作吗?我注意到的一件事是`@GET("stock/market/batch?")`末尾的?。那是不需要的。我不知道改造是否假定这是路径的一部分,或者它知道它是查询参数的开始,但我不会把它放在那里。它实际上可能会导致调用失败。
  • 你的回答是对的。我没有正确调试。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 2020-03-24
  • 1970-01-01
  • 2021-12-18
  • 2017-05-25
  • 2017-10-03
  • 2021-09-06
相关资源
最近更新 更多