gpt4 book ai didi

java - 从 Json 到 Gson 的数据模型映射返回 null

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:24 25 4
gpt4 key购买 nike

我有这个 JSON

{
"Meta Data": {
"1: Symbol": "stock",
"2: Indicator": "SMA",
"3: Last Refreshed": "2017-07-25 09:50:00",
"4: Interval": "daily",
"5: Time Period": 3,
"6: Series Type": "open",
"7: Time Zone": "US/Eastern"
},
"Technical Analysis: SMA": {
"2017-07-25 09:50:00": {
"SMA": "266.6264"
},
"2017-07-24": {
"SMA": "265.9137"
},
"2017-07-21": {
"SMA": "265.3237"
}
}}

我已经映射了这些类,以便直接使用 gson 库(Stock、MetaData、TechAnalysis、DayValue)填充模型:

public class Stock {

private MetaData metaData;
private TechAnalysis tech;


public MetaData getMetaData() {
return metaData;
}
public void setMetaData(MetaData metaData) {
this.metaData = metaData;
}
public TechAnalysis getTech() {
return tech;
}
public void setTech(TechAnalysis tech) {
this.tech = tech;
}}




public class MetaData {

private String symbol;
private String indicator;
private Date lastRefreshed;
private String interval;
private Integer period;
private String zone;



public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getIndicator() {
return indicator;
}
public void setIndicator(String indicator) {
this.indicator = indicator;
}
public Date getLastRefreshed() {
return lastRefreshed;
}
public void setLastRefreshed(Date lastRefreshed) {
this.lastRefreshed = lastRefreshed;
}
public String getInterval() {
return interval;
}
public void setInterval(String interval) {
this.interval = interval;
}
public Integer getPeriod() {
return period;
}
public void setPeriod(Integer period) {
this.period = period;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}}




public class TechAnalysis {


private List<DayValue> dayValuesList;

public List<DayValue> getDayValuesList() {
return dayValuesList;
}

public void setDayValuesList(List<DayValue> dayValuesList) {
this.dayValuesList = dayValuesList;
}}


public class DayValue {

private String value;

public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}}

我将模型映射到 JSON 响应的代码是这样的:

public static Stock getStock(String json){

Gson gson = new Gson();
return gson.fromJson(json, Stock.class);
}

但 Stock 对象始终返回 null

最佳答案

类模型和 JSON 不兼容。例如,这个 JSON ...

"Meta Data": {
"1: Symbol": "stock",
"2: Indicator": "SMA",
"3: Last Refreshed": "2017-07-25 09:50:00",
"4: Interval": "daily",
"5: Time Period": 3,
"6: Series Type": "open",
"7: Time Zone": "US/Eastern"
}

... 意味着一个类具有名为 Meta Data 的属性(这不是有效的属性名称),而“Meta Data”类需要名为 1: Symbol2: Indicator 等的属性(同样,无效的属性名称)。

您定义的类模型将与此 JSON 兼容:

{
"metaData": {
"symbol": "stock",
"indicator": "SMA",
"lastRefreshed": "2017-07-25 09:50:00",
"interval": "daily",
"period": 3,
"zone": "US/Eastern"
},
"techAnalysis": {
"dayValuesList": [
{
"value": "266.6264",
"dateTime": "2017-07-25 09:50:00"
}
]
}
}

尽管如此,也需要向 DayValue 添加 dateTime 属性。

关于java - 从 Json 到 Gson 的数据模型映射返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com