Json与Gson的数据模型映射返回null

时间:2017-07-25 14:24:20

标签: java json gson

我有这个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

1 个答案:

答案 0 :(得分:0)

类模型和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的属性(不是有效的属性名称)和“元数据”的类。 class需要名为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"
      }
    ]
  }
}

即便如此,也需要在dateTime添加DayValue属性。