我正试图从https://covidtracking.com/data/api但我有个问题。我觉得这与存储在数组中的信息有关。希望有人能把我推向写作的方向。它给我的错误是:
"JsonException:JSON值无法转换为CovidTracking.Models.CovidDataModel。路径:$|LineNumber:0|BytePositionInLine:1;
我的代码:
CovidDataModel coviddata;
string errorString;
protected override async Task OnInitializedAsync()
{
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.covidtracking.com/v1/us/current.json");
var client = _clientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
coviddata = await response.Content.ReadFromJsonAsync<CovidDataModel>();
errorString = null;
}
else
{
errorString = $"Could not load Covid Data: {response.ReasonPhrase}";
}
}
下面的模型视图:
namespace CovidTracking.Models
{
public class CovidDataModel
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public int Date { get; set; }
public int States { get; set; }
public int Positive { get; set; }
public int Negative { get; set; }
public int Hospitalized { get; set; }
public string hash { get; set; }
}
}
只需将此行更改为:
coviddata = await response.Content.ReadFromJsonAsync<List<Class1>>();
这意味着您不再需要类CovidDataModel
。
//public class CovidDataModel
//{
//public Class1[] Property1 { get; set; }
//}