Json-file (Api-Response) 反序列化为 C# Class 适用于 Winforms/Console 但我在 ASP.Net Blazor 中遇到错误!为什么?
Json-file (Api-Response) deserialized to C# Class works with Winforms/Console but I do get an Error with ASP.Net Blazor! Why?
我开始研究 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# Class 结构。 Classes 看起来像这样(第一次阅读时跳过这个):
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 项目,我可以从根模型 Class 调用数据,例如名称(class 模型中的第一个属性)
但是除了根 class 之外,我不能使用任何其他更深 class 的东西。例如。如果我想获取温度值 root->Main->Temp,则会出现错误。
我在 winforms 项目中使用了相同的 ClassModel,它是成功的。
此处的 Blazor 代码仅适用于“root class”中的属性:
@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 行,我有一个正在工作的西装外套 api 调用,并且我从 web-api- openweathermap 调用中获取城市名称。但是...
当我取消注释@weatherModel.Main.Temp 项目冻结时发生错误 Visual Studio 告诉我它在 HoldModus 中
(System.NullReferenceException:“对象引用未设置为对象的实例。”)
这发生在我浏览页面的那一刻,而不是在我点击按钮之后???知道我做错了什么吗?抱歉,post 太长而且交织在一起:)
问题是您在下一行中手动实例化 class 而未将 'Main' 属性 设置为任何值,因此它为 null
OneDayWeatherDataModel weatherModel = new OneDayWeatherDataModel();
您可以确保填充 属性 或通过以下方式访问它以避免空引用错误:
<td>@weatherModel?.Main?.Temp</td>
但是您可能还想考虑类似下面的内容,这样 table 就不会显示,除非它从 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=...");
}
}
我开始研究 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# Class 结构。 Classes 看起来像这样(第一次阅读时跳过这个):
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 项目,我可以从根模型 Class 调用数据,例如名称(class 模型中的第一个属性) 但是除了根 class 之外,我不能使用任何其他更深 class 的东西。例如。如果我想获取温度值 root->Main->Temp,则会出现错误。 我在 winforms 项目中使用了相同的 ClassModel,它是成功的。
此处的 Blazor 代码仅适用于“root class”中的属性:
@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 行,我有一个正在工作的西装外套 api 调用,并且我从 web-api- openweathermap 调用中获取城市名称。但是...
当我取消注释@weatherModel.Main.Temp 项目冻结时发生错误 Visual Studio 告诉我它在 HoldModus 中 (System.NullReferenceException:“对象引用未设置为对象的实例。”)
这发生在我浏览页面的那一刻,而不是在我点击按钮之后???知道我做错了什么吗?抱歉,post 太长而且交织在一起:)
问题是您在下一行中手动实例化 class 而未将 'Main' 属性 设置为任何值,因此它为 null
OneDayWeatherDataModel weatherModel = new OneDayWeatherDataModel();
您可以确保填充 属性 或通过以下方式访问它以避免空引用错误:
<td>@weatherModel?.Main?.Temp</td>
但是您可能还想考虑类似下面的内容,这样 table 就不会显示,除非它从 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=...");
}
}