0

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. enter image description here

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}]
5
  • 1
    Looking at the JSON structure, you could deserialize it with var movers = JsonConvert.DeserializeObject<List<Movers>>(getData); or an array (Movers[]) instead of a List. Or var movers = JArray.Parse(getData);. But you should post the actual JSON string.
    – Jimi
    Commented Jul 7, 2019 at 23:27
  • @Jimi Just updated my post with the json string. Also I tried with List<Movers> and also Movers[] as an array which yield same results. Any help is appreciated.
    – goodfella
    Commented Jul 8, 2019 at 1:47
  • 2
    Works for me. I modified the type of Change and Last properties, because those are double, not long and var movers = JsonConvert.DeserializeObject<List<Movers>>(getData); works as expected with the JSON you posted. List<Movers>, not List<MoversRoot> or MoversRoot. Newtonsoft.Json 12.0.1.
    – Jimi
    Commented Jul 8, 2019 at 2:00
  • 1
    .Net Fiddle of the results.
    – Jimi
    Commented Jul 8, 2019 at 2:12
  • @Jimi well crap :( all this time I'm fiddling with the code itself and ignored the types completely becauase I trusted Visual Studio Json to class converter or Json2class / QuickType to do the work properly creating property classes. I appreciate you taking the time to review the code. You should have posted your answer for acceptance. Thanks again
    – goodfella
    Commented Jul 8, 2019 at 11:18

1 Answer 1

2

Replace

JsonConvert.DeserializeObject<MoversRoot>(getData);

with

 JsonConvert.DeserializeObject<List<Movers>>(data);

and change the long property to double

1
  • Thanks! Jimi answered with same response. I tested and works fine. I have another Json with what looks to be a parent/root attribute I had trouble with. I'll post it here if you could walk me through proper class setup for root attribute that is also a collection of sub attributes.
    – goodfella
    Commented Jul 8, 2019 at 16:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.