I am struggling to figure out why my JsonConvert.DeserializeObject
is returning back Null. From what I understand, everything seems to be correct and I can see the Json response from GetStringAsync(myURL)
.
Property class was made through QuickType (also tried directly through Visual Studio Paste from Json class creator).
My property Class:
public partial class MoversRoot
{
List<Movers> getMovers { get; set; }
}
public partial class Movers
{
[JsonProperty("change")]
public long Change { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("direction")]
public string Direction { get; set; }
[JsonProperty("last")]
public long Last { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("totalVolume")]
public long TotalVolume { get; set; }
}
Below is my code that uses an instance of HttpClient to GetStringAsync - getData
in debug shows my Json response as it should. here is the screenshot of the Json through inspector.
However my var movers
is Null when debugging. I've tried various ways and my movers var always ends up being null. where did I go wrong?
string myURL = new Uri(@"https://api.tdameritrade.com/v1/marketdata/$SPX.X/movers?apikey=" + token).ToString();
var getData = await HttpClientInstance.Instance.GetStringAsync(myURL);
var movers = JsonConvert.DeserializeObject<MoversRoot>(getData);
Json String (per request):
[{"change":-0.043154387156591506,"description":"IPG Photonics Corporation - Common Stock","direction":"down","last":149.0,"symbol":"IPGP","totalVolume":473896},{"change":-0.04596881645392344,"description":"Electronic Arts Inc. - Common Stock","direction":"down","last":93.6,"symbol":"EA","totalVolume":11794145},{"change":-0.028465181202540747,"description":"Rockwell Automation, Inc. Common Stock","direction":"down","last":159.39,"symbol":"ROK","totalVolume":1047335},{"change":-0.003665714227582914,"description":"Apache Corporation Common Stock","direction":"down","last":27.18,"symbol":"APA","totalVolume":8024082},{"change":-0.02684707330738466,"description":"Vertex Pharmaceuticals Incorporated - Common Stock","direction":"down","last":178.34,"symbol":"VRTX","totalVolume":878875},{"change":-0.030052397132803322,"description":"Nektar Therapeutics - Common Stock","direction":"down","last":35.18,"symbol":"NKTR","totalVolume":1092229},{"change":-0.03420337996611522,"description":"Regeneron Pharmaceuticals, Inc. - Common Stock","direction":"down","last":307.5,"symbol":"REGN","totalVolume":799579},{"change":-0.022643827976298412,"description":"PulteGroup, Inc. Common Stock","direction":"down","last":31.94,"symbol":"PHM","totalVolume":3248470},{"change":-0.0190979519989881,"description":"Lennar Corporation Class A Common Stock","direction":"down","last":48.28,"symbol":"LEN","totalVolume":2755112},{"change":-0.019616651726126987,"description":"D.R. Horton, Inc. Common Stock","direction":"down","last":43.48,"symbol":"DHI","totalVolume":2445152},{"change":0.023360000000000013,"description":"Nordstrom, Inc. Common Stock","direction":"up","last":31.98,"symbol":"JWN","totalVolume":2602701},{"change":0.03426644354123586,"description":"Jefferies Financial Group Inc. Common Stock","direction":"up","last":21.43,"symbol":"JEF","totalVolume":3845367},{"change":0.021359204387494307,"description":"Foot Locker, Inc.","direction":"up","last":42.08,"symbol":"FL","totalVolume":3363962},{"change":0.02491751783290532,"description":"L Brands, Inc.","direction":"up","last":27.97,"symbol":"LB","totalVolume":4316848},{"change":0.020912577144276984,"description":"Diamondback Energy, Inc. - Commmon Stock","direction":"up","last":107.4,"symbol":"FANG","totalVolume":1356003},{"change":0.018514460830743065,"description":"Zions Bancorporation N.A. - Common Stock","direction":"up","last":46.21,"symbol":"ZION","totalVolume":1456836},{"change":0.017426412722251497,"description":"Comerica Incorporated Common Stock","direction":"up","last":72.98,"symbol":"CMA","totalVolume":1384308},{"change":0.01433982456706795,"description":"M&T Bank Corporation Common Stock","direction":"up","last":174.01,"symbol":"MTB","totalVolume":598406},{"change":0.0173953706757574,"description":"Macy's Inc Common Stock","direction":"up","last":21.64,"symbol":"M","totalVolume":5873615},{"change":0.018309164170755843,"description":"SunTrust Banks, Inc. Common Stock","direction":"up","last":63.96,"symbol":"STI","totalVolume":1821190}]
var movers = JsonConvert.DeserializeObject<List<Movers>>(getData);
or an array (Movers[]
) instead of a List. Orvar movers = JArray.Parse(getData);
. But you should post the actual JSON string.Change
andLast
properties, because those aredouble
, notlong
andvar movers = JsonConvert.DeserializeObject<List<Movers>>(getData);
works as expected with the JSON you posted.List<Movers>
, notList<MoversRoot>
orMoversRoot
.Newtonsoft.Json 12.0.1
.