如何解决反序列化为C#类的Json文件Api-Response可与Winforms / Console一起使用,但是ASP.Net Blazor却出现错误!为什么?
我开始研究Openweathermap API(天气数据)。 Api响应是一个Json文件。我设法在(VisualStudio)中使用MS WinForms正常工作。
现在,我想在BlazorWebpage上执行相同的操作。但是我无法使其正常工作。我被困住了,也许您可以指出正确的方向。
在Winforms项目中,我使用以下代码成功调用了Api:
private async Task CallWeatherApiAsync()
{
oneDayWeatherInfo = new OneDayWeatherDataModel();
using (HttpResponseMessage response = await client.GetAsync(client.BaseAddress))
{
if (response.IsSuccessStatusCode)
{
oneDayWeatherInfo= await response.Content.ReadAsAsync<OneDayWeatherDataModel>();
}
}
}
api响应数据中的所有内容都在正确的位置“显示”。 WeatherDataModel是来自jsonfile的C#类结构。看起来像这样的类(在初读时跳过):
namespace Blazor_WeatherApi.Models
{
public class OneDayWeatherDataModel
{
//Name of the City
public string Name { get; set; }
public Main Main { get; set; }
public Coord Coord { get; set; }
public List<Weather> Weather { get; set; }
public Wind Wind { get; set; }
public Clouds Clouds { get; set; }
public Sys Sys { get; set; }
public int Visibility { get; set; }
public int Dt { get; set; }
public int Timezone { get; set; }
public int Id { get; set; }
public int Cod { get; set; }
}
public class Main
{
public double Temp { get; set; }
public double Feels_like { get; set; }
public double Temp_min { get; set; }
public double Temp_max { get; set; }
public int Pressure { get; set; }
public int Humidity { get; set; }
}
public class Coord
{
public double Lon { get; set; }
public double Lat { get; set; }
}
public class Weather
{
public int Id { get; set; }
public string Main { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
}
public class Wind
{
public double Speed { get; set; }
public int Deg { get; set; }
public double Gust { get; set; }
}
public class Clouds
{
public int All { get; set; }
}
public class Sys
{
public int Type { get; set; }
public int Id { get; set; }
public double Message { get; set; }
public string Country { get; set; }
public int Sunrise { get; set; }
public int Sunset { get; set; }
}
}
由于我的问题,我创建了一个blazor项目,并且可以从根ModelClass调用数据,例如Name(类Model中的第一个属性) 但是我不能使用除根类之外更深一层的任何其他东西。例如。如果我想获得温度值root-> Main-> Temp,则会出现错误。 在成功的winforms项目中使用了相同的ClassModel。
以下Blazor代码仅适用于“根类”中的“属性”:
@page "/weather"
@inject HttpClient http;
<h3>ApiCall</h3>
<button class="btn btn-primary" @onclick="@GetWeatherData">Get Weather</button>
<table>
<tr>
<td>@weatherModel.Name</td>
</tr>
<tr>
<td>@weatherModel.Dt</td>
</tr>
@*<tr>
<td>@weatherModel.Main.Temp</td> // IF I DECOMMENT THIS 3 LINES I GET the ERROR
</tr>*@
</table>
@code {
OneDayWeatherDataModel weatherModel = new OneDayWeatherDataModel();
private async Task GetWeatherData()
{
weatherModel = await http.GetJsonAsync<OneDayWeatherDataModel>
("https://api.openweathermap.org/data/2.5/weather?id=...");
}
}
如果我将3行注释掉,则我有一个有效的blazer api调用,并且从openweathermap的web-api-call中获得了城市名称。但是...
当我将@ weatherModel.Main.Temp分解为项目时,发生错误,该项目冻结了Visual Studio,告诉我它在HoldModus中 (System.NullReferenceException:“对象引用未设置为对象的实例。”)
这是在我单击按钮而不是浏览页面时发生的?知道我做错了什么吗?很抱歉,这篇文章是如此冗长而纠结:)
解决方法
问题是您要手动在下面的行中实例化该类,而不是将'Main'属性设置为任何值,因此它为null
OneDayWeatherDataModel weatherModel =新的OneDayWeatherDataModel();
您可以确保填充属性或使用以下内容访问属性,以避免出现空引用错误:
<td>@weatherModel?.Main?.Temp</td>
但是,您可能还需要考虑以下内容,以便除非从API获取数据,否则该表不会显示:
@if (weatherModel != null) {
<table>
<tr>
<td>@weatherModel.Name</td>
</tr>
<tr>
<td>@weatherModel.Dt</td>
</tr>
<tr>
<td>@weatherModel.Main.Temp</td>
</tr>
</table>
}
@code {
OneDayWeatherDataModel weatherModel = null;
private async Task GetWeatherData()
{
weatherModel = await http.GetJsonAsync<OneDayWeatherDataModel>
("https://api.openweathermap.org/data/2.5/weather?id=...");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。