当JSON数据是动态的时,如何在Android中进行改进?
我正在制作一个向用户显示每日股票数据的应用程序。问题是,我正在使用改型来进行API调用。对于这种类型的JSON,我需要一个带有每个股票符号的POJO。这显然是不可能的。 我希望能够按照-当JSON数据是动态的时,如何在Android中进行改进?,android,json,gson,retrofit,pojo,Android,Json,Gson,Retrofit,Pojo,我正在制作一个向用户显示每日股票数据的应用程序。问题是,我正在使用改型来进行API调用。对于这种类型的JSON,我需要一个带有每个股票符号的POJO。这显然是不可能的。 我希望能够按照-listOfStocks[1].getQuote().getLatestPrice()的思路做一些事情 这就是我正在接收的JSON数据 { "AAPL":{ "quote":{ "symbol":"AAPL", "companyName":"App
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
下面是一个示例,其中模型只有latestPrice
属性来缩短示例:
class Quote {
@SerializedName("latestPrice")
private double latestPrice;
// ...
}
class Symbol {
@SerializedName("quote")
private Quote quote;
//...
}
现在使用Map
进行改装调用应该会给您一个映射,您可以在其中迭代条目并获得每个条目的最新价格。您可以详细解释问题的具体原因吗?我是使用“Map”还是“List”进行调用?Map'
-json返回一个对象作为根元素。改型可以将其映射为字符串到符号的映射。如果您可以查看它并给出建议,那就太好了。这里是链接。谢谢差不多就是这样。它不起作用吗?我注意到的一件事是“@GET”(“stock/market/batch?”)结尾的?
”。这是不必要的。我不知道改造是否假定这是路径的一部分,或者它知道这是查询参数的开始,但我不会把它放在那里。事实上,这可能会导致电话失败。你的回答是对的。我没有正确地调试。非常感谢。