我正在努力找出为什么我的JsonConvert.DeserializeObject
返回Null的原因。据我了解,一切似乎都是正确的,我可以看到GetStringAsync(myURL)
的Json回应。
属性类是通过QuickType创建的(也可以通过Json类创建者的Visual Studio Paste直接尝试)。
我的财产分类:
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; }
}
下面是我的代码,该代码使用HttpClient的实例获取GetStringAsync-调试中的getData
显示了我的Json响应。这是通过检查器的Json的屏幕截图。
但是调试时我的var movers
为Null。我尝试了各种方法,而我的movers var总是以null结尾。我哪里出问题了?
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(每个请求):
[{"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}]
答案 0 :(得分:1)
替换
JsonConvert.DeserializeObject<MoversRoot>(getData);
与
JsonConvert.DeserializeObject<List<Movers>>(data);
并将long属性更改为double