- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Jackson 将 JSON 映射到 POJO。但是,由于此 JSON 包含对象的嵌套映射,因此当我将其反序列化为 POJO 时,时间序列信息不会转换为 POJO。我只能获取时间序列 block 中的元数据部分和日期部分。 timeseries block 中的其他字段(例如开盘价、最高价和最低价)始终为空。
jackson 似乎无法匹配 TimeSeries 类中的字段。有人可以告诉我应该如何做或指出正确的方向吗?或者是否有其他更好的方法来做到这一点。谢谢!
这是 JSON 的示例
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2019-02-15",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2019-02-15": {
"1. open": "107.9100",
"2. high": "108.3000",
"3. low": "107.3624",
"4. close": "108.2200",
"5. volume": "26606886"
},
"2019-02-14": {
"1. open": "106.3100",
"2. high": "107.2900",
"3. low": "105.6600",
"4. close": "106.9000",
"5. volume": "21784703"
}
}
}
现在,为了映射此 JSON,我创建了这些 POJO
@JsonIgnoreProperties(ignoreUnknown = true)
public class HistoricalStock {
@JsonProperty("Meta Data")
private MetaData metadata;
private Map<String, TimeSeriesInfo> stockDailyData = new HashMap<String, TimeSeriesInfo>();
public HistoricalStock() {
}
public MetaData getMetadata() {
return metadata;
}
public void setMetadata(MetaData metadata) {
this.metadata = metadata;
}
@JsonAnyGetter
public Map<String, TimeSeriesInfo> getStockDailyData() {
return stockDailyData;
}
@JsonAnySetter
public void setStockDailyData(String date, TimeSeriesInfo stockInfo) {
this.stockDailyData.put(date, stockInfo);
}
@Override
public String toString() {
return "HistoricalStock [metadata=" + metadata + ", stockDailyData=" + stockDailyData + "]";
}
}
这是使用 Jackson 反序列化 JSON 的代码。
String fooResourceUrl = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=DEMO";
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
ObjectMapper customMapper = new ObjectMapper();
try {
HistoricalStock msft = customMapper.readValue(response.getBody(), HistoricalStock.class);
System.out.println(msft.getMetadata());
System.out.println(msft.getStockDailyData().toString());
} catch (IOException ioException) {
ioException.printStackTrace();
}
这是 TimeSeries 类的代码
@JsonIgnoreProperties(ignoreUnknown = true)
public class TimeSeriesInfo {
@JsonProperty("1. open")
private Double openingPrice;
@JsonProperty("2. high")
private Double highestPrice;
@JsonProperty("3. low")
private Double lowestPrice;
@JsonProperty("4. close")
private Double closingPrice;
@JsonProperty("5. volume")
private Long volume;
public TimeSeriesInfo() {
}
public Double getOpeningPrice() {
return openingPrice;
}
public void setOpeningPrice(Double openingPrice) {
this.openingPrice = openingPrice;
}
public Double getHighestPrice() {
return highestPrice;
}
public void setHighestPrice(Double highestPrice) {
this.highestPrice = highestPrice;
}
public Double getLowestPrice() {
return lowestPrice;
}
public void setLowestPrice(Double lowestPrice) {
this.lowestPrice = lowestPrice;
}
public Double getClosingPrice() {
return closingPrice;
}
public void setClosingPrice(Double closingPrice) {
this.closingPrice = closingPrice;
}
public Long getVolume() {
return volume;
}
public void setVolume(Long volume) {
this.volume = volume;
}
@Override
public String toString() {
return "TimeSeries [openingPrice=" + openingPrice + ", highestPrice=" + highestPrice + ", lowestPrice="
+ lowestPrice + ", closingPrice=" + closingPrice + ", volume=" + volume + "]";
}
}
最佳答案
在此特定示例中,您不需要使用 @JsonAnyGetter
和 @JsonAnySetter
注释。只需创建一个 Map<String, TimeSeriesInfo>
属性,它就应该可以正常工作。另外,我建议使用 BigDecimal
而不是 Double
和 Long
。下面您可以找到整个 POJO
的结构,无需任何额外的注释即可正常工作:
class DailySeries {
@JsonProperty("Meta Data")
private Metadata metadata;
@JsonProperty("Time Series (Daily)")
private Map<String, Daily> series;
public Metadata getMetadata() {
return metadata;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
public Map<String, Daily> getSeries() {
return series;
}
public void setSeries(Map<String, Daily> series) {
this.series = series;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
String lineSeparator = System.lineSeparator();
sb.append("metadata=").append(metadata).append(lineSeparator);
series.forEach((k, s) -> sb.append(k).append(" = ").append(s).append(lineSeparator));
return sb.toString();
}
}
class Metadata {
@JsonProperty("1. Information")
private String information;
@JsonProperty("2. Symbol")
private String symbol;
@JsonProperty("3. Last Refreshed")
private String lastRefreshed;
@JsonProperty("4. Output Size")
private String outputSize;
@JsonProperty("5. Time Zone")
private String timeZone;
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getLastRefreshed() {
return lastRefreshed;
}
public void setLastRefreshed(String lastRefreshed) {
this.lastRefreshed = lastRefreshed;
}
public String getOutputSize() {
return outputSize;
}
public void setOutputSize(String outputSize) {
this.outputSize = outputSize;
}
public String getTimeZone() {
return timeZone;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
@Override
public String toString() {
return "Metadata{" +
"information='" + information + '\'' +
", symbol='" + symbol + '\'' +
", lastRefreshed='" + lastRefreshed + '\'' +
", outputSize='" + outputSize + '\'' +
", timeZone='" + timeZone + '\'' +
'}';
}
}
class Daily {
@JsonProperty("1. open")
private BigDecimal open;
@JsonProperty("2. high")
private BigDecimal high;
@JsonProperty("3. low")
private BigDecimal low;
@JsonProperty("4. close")
private BigDecimal close;
@JsonProperty("5. volume")
private BigDecimal volume;
public BigDecimal getOpen() {
return open;
}
public void setOpen(BigDecimal open) {
this.open = open;
}
public BigDecimal getHigh() {
return high;
}
public void setHigh(BigDecimal high) {
this.high = high;
}
public BigDecimal getLow() {
return low;
}
public void setLow(BigDecimal low) {
this.low = low;
}
public BigDecimal getClose() {
return close;
}
public void setClose(BigDecimal close) {
this.close = close;
}
public BigDecimal getVolume() {
return volume;
}
public void setVolume(BigDecimal volume) {
this.volume = volume;
}
@Override
public String toString() {
return "Daily{" +
"open=" + open +
", high=" + high +
", low=" + low +
", close=" + close +
", volume=" + volume +
'}';
}
}
使用示例:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.math.BigDecimal;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(jsonFile, DailySeries.class));
}
}
以上代码有效:
metadata=Metadata{information='Daily Prices (open, high, low, close) and Volumes', symbol='MSFT', lastRefreshed='2019-02-15', outputSize='Compact', timeZone='US/Eastern'}
2019-02-15 = Daily{open=107.9100, high=108.3000, low=107.3624, close=108.2200, volume=26606886}
2019-02-14 = Daily{open=106.3100, high=107.2900, low=105.6600, close=106.9000, volume=21784703}
2019-02-13 = Daily{open=107.5000, high=107.7800, low=106.7100, close=106.8100, volume=18394869}
2019-02-12 = Daily{open=106.1400, high=107.1400, low=105.4800, close=106.8900, volume=25056595}
2019-02-11 = Daily{open=106.2000, high=106.5800, low=104.9650, close=105.2500, volume=18914123}
2019-02-08 = Daily{open=104.3900, high=105.7800, low=104.2603, close=105.6700, volume=21461093}
2019-02-07 = Daily{open=105.1850, high=105.5900, low=104.2900, close=105.2700, volume=29760697}
2019-02-06 = Daily{open=107.0000, high=107.0000, low=105.5300, close=106.0300, volume=20609759}
2019-02-05 = Daily{open=106.0600, high=107.2700, low=105.9600, close=107.2200, volume=27325365}
2019-02-04 = Daily{open=102.8700, high=105.8000, low=102.7700, close=105.7400, volume=31315282}
2019-02-01 = Daily{open=103.7750, high=104.0999, low=102.3500, close=102.7800, volume=35535690}
2019-01-31 = Daily{open=103.8000, high=105.2200, low=103.1800, close=104.4300, volume=55636391}
2019-01-30 = Daily{open=104.6200, high=106.3800, low=104.3300, close=106.3800, volume=49471866}
2019-01-29 = Daily{open=104.8800, high=104.9700, low=102.1700, close=102.9400, volume=31490547}
2019-01-28 = Daily{open=106.2600, high=106.4800, low=104.6600, close=105.0800, volume=29476719}
2019-01-25 = Daily{open=107.2400, high=107.8800, low=106.5900, close=107.1700, volume=31218193}
2019-01-24 = Daily{open=106.8600, high=107.0000, low=105.3400, close=106.2000, volume=23164838}
2019-01-23 = Daily{open=106.1200, high=107.0400, low=105.3400, close=106.7100, volume=25874294}
2019-01-22 = Daily{open=106.7500, high=107.1000, low=104.8600, close=105.6800, volume=32371253}
2019-01-18 = Daily{open=107.4600, high=107.9000, low=105.9100, close=107.7100, volume=37427587}
2019-01-17 = Daily{open=105.0000, high=106.6250, low=104.7600, close=106.1200, volume=28393015}
2019-01-16 = Daily{open=105.2600, high=106.2550, low=104.9600, close=105.3800, volume=29853865}
2019-01-15 = Daily{open=102.5100, high=105.0500, low=101.8800, close=105.0100, volume=31587616}
2019-01-14 = Daily{open=101.9000, high=102.8716, low=101.2600, close=102.0500, volume=28437079}
2019-01-11 = Daily{open=103.1900, high=103.4400, low=101.6400, close=102.8000, volume=28314202}
2019-01-10 = Daily{open=103.2200, high=103.7500, low=102.3800, close=103.6000, volume=30067556}
2019-01-09 = Daily{open=103.8600, high=104.8800, low=103.2445, close=104.2700, volume=32280840}
2019-01-08 = Daily{open=103.0400, high=103.9700, low=101.7134, close=102.8000, volume=31514415}
2019-01-07 = Daily{open=101.6400, high=103.2681, low=100.9800, close=102.0600, volume=35656136}
2019-01-04 = Daily{open=99.7200, high=102.5100, low=98.9300, close=101.9300, volume=44060620}
2019-01-03 = Daily{open=100.1000, high=100.1850, low=97.2000, close=97.4000, volume=42578410}
2019-01-02 = Daily{open=99.5500, high=101.7500, low=98.9400, close=101.1200, volume=35329345}
2018-12-31 = Daily{open=101.2900, high=102.4000, low=100.4400, close=101.5700, volume=33173765}
2018-12-28 = Daily{open=102.0900, high=102.4100, low=99.5200, close=100.3900, volume=38169312}
2018-12-27 = Daily{open=99.3000, high=101.1900, low=96.4000, close=101.1800, volume=49498509}
2018-12-26 = Daily{open=95.1400, high=100.6900, low=93.9600, close=100.5600, volume=51634793}
2018-12-24 = Daily{open=97.6800, high=97.9700, low=93.9800, close=94.1300, volume=43935192}
2018-12-21 = Daily{open=101.6300, high=103.0000, low=97.4600, close=98.2300, volume=111242070}
2018-12-20 = Daily{open=103.0500, high=104.3100, low=98.7800, close=101.5100, volume=70334184}
2018-12-19 = Daily{open=103.6500, high=106.8800, low=101.3500, close=103.6900, volume=68198186}
2018-12-18 = Daily{open=103.7500, high=104.5100, low=102.5200, close=103.9700, volume=49319196}
2018-12-17 = Daily{open=105.4100, high=105.8000, low=101.7100, close=102.8900, volume=56957314}
2018-12-14 = Daily{open=108.2500, high=109.2600, low=105.5000, close=106.0300, volume=47043136}
2018-12-13 = Daily{open=109.5800, high=110.8700, low=108.6300, close=109.4500, volume=31333362}
2018-12-12 = Daily{open=110.8900, high=111.2700, low=109.0400, close=109.0800, volume=36183020}
2018-12-11 = Daily{open=109.8000, high=110.9500, low=107.4400, close=108.5900, volume=42381947}
2018-12-10 = Daily{open=104.8000, high=107.9800, low=103.8900, close=107.5900, volume=40801525}
2018-12-07 = Daily{open=108.3800, high=109.4500, low=104.3000, close=104.8200, volume=45044937}
2018-12-06 = Daily{open=105.8200, high=109.2400, low=105.0000, close=109.1900, volume=49107431}
2018-12-04 = Daily{open=111.9400, high=112.6373, low=108.2115, close=108.5200, volume=45196984}
2018-12-03 = Daily{open=113.0000, high=113.4200, low=110.7300, close=112.0900, volume=34732772}
2018-11-30 = Daily{open=110.7000, high=110.9700, low=109.3600, close=110.8900, volume=33665624}
2018-11-29 = Daily{open=110.3300, high=111.1150, low=109.0300, close=110.1900, volume=28123195}
2018-11-28 = Daily{open=107.8900, high=111.3300, low=107.8600, close=111.1200, volume=46788461}
2018-11-27 = Daily{open=106.2700, high=107.3300, low=105.3600, close=107.1400, volume=29124486}
2018-11-26 = Daily{open=104.7900, high=106.6300, low=104.5800, close=106.4700, volume=32336165}
2018-11-23 = Daily{open=102.1700, high=103.8099, low=102.0000, close=103.0700, volume=13823099}
2018-11-21 = Daily{open=103.6000, high=104.4300, low=102.2400, close=103.1100, volume=28130621}
2018-11-20 = Daily{open=101.8000, high=102.9700, low=99.3528, close=101.7100, volume=64052457}
2018-11-19 = Daily{open=108.2700, high=108.5600, low=103.5500, close=104.6200, volume=44773899}
2018-11-16 = Daily{open=107.0800, high=108.8800, low=106.8000, close=108.2900, volume=33502121}
2018-11-15 = Daily{open=104.9900, high=107.8000, low=103.9100, close=107.2800, volume=38505165}
2018-11-14 = Daily{open=108.1000, high=108.2600, low=104.4700, close=104.9700, volume=39495141}
2018-11-13 = Daily{open=107.5500, high=108.7400, low=106.6400, close=106.9400, volume=35374583}
2018-11-12 = Daily{open=109.4200, high=109.9600, low=106.1000, close=106.8700, volume=33621807}
2018-11-09 = Daily{open=110.8500, high=111.4500, low=108.7600, close=109.5700, volume=32039223}
2018-11-08 = Daily{open=111.8000, high=112.2100, low=110.9100, close=111.7500, volume=25644105}
2018-11-07 = Daily{open=109.4400, high=112.2400, low=109.4000, close=111.9600, volume=37901704}
2018-11-06 = Daily{open=107.3800, high=108.8400, low=106.2800, close=107.7200, volume=24340248}
2018-11-05 = Daily{open=106.3700, high=107.7400, low=105.9000, close=107.5100, volume=27922144}
2018-11-02 = Daily{open=106.4800, high=107.3200, low=104.9750, close=106.1600, volume=37680194}
2018-11-01 = Daily{open=107.0500, high=107.3200, low=105.5300, close=105.9200, volume=33384201}
2018-10-31 = Daily{open=105.4350, high=108.1400, low=105.3900, close=106.8100, volume=51062383}
2018-10-30 = Daily{open=103.6600, high=104.3800, low=100.1100, close=103.7300, volume=65350878}
2018-10-29 = Daily{open=108.1050, high=108.7000, low=101.6300, close=103.8500, volume=55162001}
2018-10-26 = Daily{open=105.6900, high=108.7500, low=104.7600, close=106.9600, volume=55523104}
2018-10-25 = Daily{open=106.5500, high=109.2700, low=106.1500, close=108.3000, volume=61646819}
2018-10-24 = Daily{open=108.4100, high=108.4900, low=101.5901, close=102.3200, volume=63897759}
2018-10-23 = Daily{open=107.7700, high=108.9700, low=105.1100, close=108.1000, volume=43770429}
2018-10-22 = Daily{open=109.3200, high=110.5400, low=108.2400, close=109.6300, volume=26545607}
2018-10-19 = Daily{open=108.9300, high=110.8600, low=108.2100, close=108.6600, volume=32785475}
2018-10-18 = Daily{open=110.1000, high=110.5300, low=107.8300, close=108.5000, volume=32506192}
2018-10-17 = Daily{open=111.6800, high=111.8100, low=109.5482, close=110.7100, volume=26548243}
2018-10-16 = Daily{open=109.5400, high=111.4100, low=108.9500, close=111.0000, volume=31610164}
2018-10-15 = Daily{open=108.9100, high=109.4800, low=106.9468, close=107.6000, volume=32068103}
2018-10-12 = Daily{open=109.0100, high=111.2400, low=107.1200, close=109.5700, volume=47742109}
2018-10-11 = Daily{open=105.3500, high=108.9300, low=104.2000, close=105.9100, volume=63904282}
2018-10-10 = Daily{open=111.2400, high=111.5000, low=105.7900, close=106.1600, volume=61376300}
2018-10-09 = Daily{open=111.1400, high=113.0800, low=110.8000, close=112.2600, volume=26198594}
2018-10-08 = Daily{open=111.6600, high=112.0300, low=109.3400, close=110.8500, volume=29640588}
2018-10-05 = Daily{open=112.6300, high=113.1700, low=110.6400, close=112.1300, volume=29068859}
2018-10-04 = Daily{open=114.6100, high=114.7588, low=111.6300, close=112.7900, volume=34821717}
2018-10-03 = Daily{open=115.4200, high=116.1800, low=114.9300, close=115.1700, volume=16648018}
2018-10-02 = Daily{open=115.3000, high=115.8400, low=114.4400, close=115.1500, volume=20787239}
2018-10-01 = Daily{open=114.7500, high=115.6800, low=114.7300, close=115.6100, volume=18883079}
2018-09-28 = Daily{open=114.1900, high=114.5700, low=113.6800, close=114.3700, volume=21647811}
2018-09-27 = Daily{open=114.7800, high=114.9100, low=114.2000, close=114.4100, volume=19091299}
2018-09-26 = Daily{open=114.4700, high=115.0550, low=113.7400, close=113.9800, volume=19352025}
2018-09-25 = Daily{open=114.8000, high=115.1000, low=113.7500, close=114.4500, volume=22668014}
2018-09-24 = Daily{open=113.0300, high=114.9000, low=112.2175, close=114.6700, volume=27334460}
以上代码是使用 Jackson
版本中的 2.9.8
进行测试
关于java - 将 JSON 映射到 POJO 时获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54739081/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!