Why does JsonConvert.DeserializeObject return an error: Exception: Value cannot be null. (Parameter 'source')
Why does JsonConvert.DeserializeObject return an error: Exception: Value cannot be null. (Parameter 'source')
我有一个 public async Task GetCompaniesAsync()
编码如下:
public async Task GetCompaniesAsync()
{
_getCompaniesSuccessful = false;
var result = await _http.GetAsync($"https://somesite.com/api/companies");
if (result.IsSuccessStatusCode)
{
// I split the code after the suggestion of @mjwills (thanks)
var content = await result.Content.ReadAsStringAsync();
var obj = JsonConvert.DeserializeObject<StockMarket>(content).Companies;
// at this point, content.Length has a value (e.g. 24429)
// but, obj is null (after JsonConvert)
if (obj != null)
{
_companies = obj.ToList();
_getCompaniesSuccessful = true;
}
}
在其他一些 class 中,我使用以下代码:
await MyService.GetCompaniesAsync();
if (MyService.GetCompaniesSuccessful)
{
foreach (var record in MyService.Companies)
{
await Context.Channel.SendMessageAsync($"{record.TickerSymbol,-10}\t{record.CompanyName,-10}");
}
}
但是当我收到以下错误时:异常:值不能为空。 (参数'source')
难道它还没有取完数据?请帮忙...
顺便说一下,webapi url returns JSON 数据,它看起来像这样:
{
"companies": [
{
"ticker": "HEY",
"name": "Hey Corporation",
"status": "open"
},
{
"ticker": "PER",
"name": "Pears Corporation",
"status": "close"
},
{
"ticker": "BRGR",
"name": "Burger Inc.",
"status": "open"
},
]
}
这些是 StockMarket 和 Company classes:
public class StockMarket
{
public StockMarket()
{
}
public ICollection<Company> Companies { get; set; }
}
public class Company
{
public Company()
{
TickerSymbol = "";
CompanyName = "";
Status = "";
}
public string TickerSymbol { get; set; }
public string CompanyName { get; set; }
public string Status { get; set; }
}
查看示例,您的 JSON 有一个“名称”键,但您的公司 class 的 属性 名为“CompanyName”。
尝试将 属性 更改为简单的名称,或者用这个
装饰它
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
此外,TickerSymbol 属性也是如此。
我有一个 public async Task GetCompaniesAsync()
编码如下:
public async Task GetCompaniesAsync()
{
_getCompaniesSuccessful = false;
var result = await _http.GetAsync($"https://somesite.com/api/companies");
if (result.IsSuccessStatusCode)
{
// I split the code after the suggestion of @mjwills (thanks)
var content = await result.Content.ReadAsStringAsync();
var obj = JsonConvert.DeserializeObject<StockMarket>(content).Companies;
// at this point, content.Length has a value (e.g. 24429)
// but, obj is null (after JsonConvert)
if (obj != null)
{
_companies = obj.ToList();
_getCompaniesSuccessful = true;
}
}
在其他一些 class 中,我使用以下代码:
await MyService.GetCompaniesAsync();
if (MyService.GetCompaniesSuccessful)
{
foreach (var record in MyService.Companies)
{
await Context.Channel.SendMessageAsync($"{record.TickerSymbol,-10}\t{record.CompanyName,-10}");
}
}
但是当我收到以下错误时:异常:值不能为空。 (参数'source') 难道它还没有取完数据?请帮忙...
顺便说一下,webapi url returns JSON 数据,它看起来像这样:
{
"companies": [
{
"ticker": "HEY",
"name": "Hey Corporation",
"status": "open"
},
{
"ticker": "PER",
"name": "Pears Corporation",
"status": "close"
},
{
"ticker": "BRGR",
"name": "Burger Inc.",
"status": "open"
},
]
}
这些是 StockMarket 和 Company classes:
public class StockMarket
{
public StockMarket()
{
}
public ICollection<Company> Companies { get; set; }
}
public class Company
{
public Company()
{
TickerSymbol = "";
CompanyName = "";
Status = "";
}
public string TickerSymbol { get; set; }
public string CompanyName { get; set; }
public string Status { get; set; }
}
查看示例,您的 JSON 有一个“名称”键,但您的公司 class 的 属性 名为“CompanyName”。
尝试将 属性 更改为简单的名称,或者用这个
装饰它[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
此外,TickerSymbol 属性也是如此。