java - Data model mapping from Json with Gson returns null -


i have 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 model filled directly gson library (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 map model on json response this:

public static stock getstock(string json){      gson gson = new gson();       return gson.fromjson(json, stock.class);  } 

but stock object returns null

the class model , json not compatible. example, 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 class attribute named meta data (which not valid attribute name) , 'meta data' class require attributes named 1: symbol, 2: indicator etc (again, invalid attribute names).

the class model defined compatible 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 require addition of datetime attribute dayvalue.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -