[英]Data model mapping from Json with Gson returns null
I have this 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"
},
"Technical Analysis: SMA": {
"2017-07-25 09:50:00": {
"SMA": "266.6264"
},
"2017-07-24": {
"SMA": "265.9137"
},
"2017-07-21": {
"SMA": "265.3237"
}
}}
and I've mapped these classes in order to get model filled directly with gson library (Stock, MetaData, TechAnalysis, DayValue): 并且我已经映射了这些类,以便直接用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;
}}
My code to map the model on the JSON response is this: 我将代码映射到JSON响应上的代码是这样的:
public static Stock getStock(String json){
Gson gson = new Gson();
return gson.fromJson(json, Stock.class);
}
but the Stock object returns always null 但是Stock对象始终返回null
The class model and the JSON are not compatible. 类模型和JSON不兼容。 For example, this 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"
}
... implies a class with an attribute named Meta Data
(which is not a valid attribute name) and the 'Meta Data' class would require attributes named 1: Symbol
, 2: Indicator
etc (again, invalid attribute names). ...表示一个类的属性名为
Meta Data
(不是有效的属性名称),“ Meta Data”类将需要名为1: Symbol
属性, 2: Indicator
等(同样,无效的属性名)。
The class model you defined would be compatible with this JSON: 您定义的类模型将与此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"
}
]
}
}
Though even that would require the addition of a dateTime
attribute to DayValue
. 尽管即使那样,也需要在
DayValue
添加dateTime
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.