android - 当 JSON 数据是动态的时,如何在 Android 中进行改造?
问题描述
我正在制作一个向用户显示每日股票数据的应用程序。问题是,我正在使用改造来进行 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
}
}
}
解决方案
就个人而言,我会为它制作一个模型quote
,symbol
然后使用Map<String, Symbol>
.
这是一个示例,其中模型仅具有latestPrice
缩短示例的属性:
class Quote {
@SerializedName("latestPrice")
private double latestPrice;
// ...
}
class Symbol {
@SerializedName("quote")
private Quote quote;
//...
}
现在使用 Retrofit 调用Map<String, Symbol>
应该会给你一个地图,你可以在其中迭代条目并获取每个条目的最新价格。
推荐阅读
- qt - 最小化和最大化按钮不起作用,但通过 alt+space 菜单最大化/最小化有效
- java - 如何决定在 JPA 中应该使用哪种实体关系
- jenkins - Jenkins Pipeline 通过 cifs 发布,pyinstaller 生成的文件不复制
- mysql - mysql查询中的两个select语句
- javascript - 为什么数组有一个给定的属性长度,为什么它不在原型中?
- postgresql - 如何根据条件分离和计算一列?
- android - WorkManager 不重复 PeriodicWorkRequest
- vue.js - 我无法在 axios 中捕获 response.data
- oracle - 如何在oracle中执行表格中的数据以形成表格?
- nuget - 本地源的 NuGet“找不到包”