我正在将JSON对象反序列化为使用json2csharp创建的类。反序列化后,我将无法访问该对象的嵌套值。我得到的错误是这样的:
System.NullReferenceException:'对象引用未设置为对象的实例。'
这是我创建的课程;
public class Ticker
{
public List<object> error { get; set; }
public Result result { get; set; }
public class Result
{
public XXBTZCAD XBTCAD { get; set; }
public class XXBTZCAD
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
}
}
在撰写本文时,这是JSON字符串:
{
"error": [
],
"result": {
"XXBTZCAD": {
"a": [
"4776.10000",
"8",
"8.000"
],
"b": [
"4773.90000",
"1",
"1.000"
],
"c": [
"4776.20000",
"0.02510294"
],
"v": [
"4.85183508",
"61.15237421"
],
"p": [
"4775.02293",
"4767.92898"
],
"t": [
126,
608
],
"l": [
"4756.70000",
"4720.60000"
],
"h": [
"4780.00000",
"4783.30000"
],
"o": "4776.40000"
}
}
}
这是反序列化并尝试显示List a的第一个值的代码
private async void GetKraken(object sender, RoutedEventArgs e)
{
var client = new RestClient();
IRestResponse response;
Ticker BTCCADTicker = new Ticker();
client.BaseUrl = new Uri("https://api.kraken.com/0/public/Ticker?pair=XBTCAD");
var request = new RestRequest();
response = await client.ExecuteTaskAsync(request);
BTCCADTicker = JsonConvert.DeserializeObject<Ticker>(response.Content);
MessageBox.Show(BTCCADTicker.result.XBTCAD.a[0]);
}
我希望看到返回一个大约4700的值,但是我得到了上面的错误。是什么赋予了?任何帮助表示赞赏。
答案 0 :(得分:0)
您尚未在模型中定义XXBTZCAD
。您误将其称为XBTCAD
。修复它看起来像这样:
public class Ticker
{
public List<object> error { get; set; }
public Result result { get; set; }
public class Result
{
public XXBTZCAD XXBTZCAD { get; set; }
public class XXBTZCAD
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
}
}
我现在看到的是与嵌套类相同的名称。我建议不要嵌套类的简单解决方案:
public class Ticker
{
public List<object> error { get; set; }
public Result result { get; set; }
}
public class Result
{
public XXBTZCAD XXBTZCAD { get; set; }
}
public class XXBTZCAD
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
如果您确实要嵌套它们,只需将class XXBTZCAD
重命名为其他名称。对于反序列化而言,重要的是属性名称,而不是类型名称。
答案 1 :(得分:0)
我建议在Visual Studio中复制您的JSON:
编辑>选择性粘贴>将JSON解析为类
public class Ticker
{
public object[] error { get; set; }
public Result result { get; set; }
}
public class Result
{
public XXBTZCAD XXBTZCAD { get; set; }
}
public class XXBTZCAD
{
public string[] a { get; set; }
public string[] b { get; set; }
public string[] c { get; set; }
public string[] v { get; set; }
public string[] p { get; set; }
public int[] t { get; set; }
public string[] l { get; set; }
public string[] h { get; set; }
public string o { get; set; }
}
,您可以使用此方法获取结果:
public static async Task<string> GetKraken()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://api.kraken.com/0/public/Ticker?pair=XBTCAD");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
然后使用该方法并将其转换为实际对象:
var ticker = JsonConvert.DeserializeObject<Ticker>(GetKraken().Result);