Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反序列化为C#类的Json文件(Api响应)可用于Winforms/Console,但我在ASP.NETBlazor中确实遇到了一个错误!为什么?_C#_Asp.net_Api_Blazor_Json Deserialization - Fatal编程技术网

反序列化为C#类的Json文件(Api响应)可用于Winforms/Console,但我在ASP.NETBlazor中确实遇到了一个错误!为什么?

反序列化为C#类的Json文件(Api响应)可用于Winforms/Console,但我在ASP.NETBlazor中确实遇到了一个错误!为什么?,c#,asp.net,api,blazor,json-deserialization,C#,Asp.net,Api,Blazor,Json Deserialization,我开始研究OpenWeatherMapAPI(天气数据)。Api响应是一个Json文件。我设法在(VisualStudio)中与WinForms女士一起工作 现在我想在我的BlazorWebpage上做同样的事情。但是我不能让它工作。我被困住了也许你能给我指出正确的方向 在Winforms项目中,我成功使用以下代码调用Api: private async Task CallWeatherApiAsync() { oneDayWeatherInfo = new On

我开始研究OpenWeatherMapAPI(天气数据)。Api响应是一个Json文件。我设法在(VisualStudio)中与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>(); 
            }
        }
     }
private async Task callweatherapiaasync()
{
oneDayWeatherInfo=新的OneDayWeatherDataModel();
使用(httpresponsemessageresponse=wait client.GetAsync(client.BaseAddress))
{
if(响应。IsSuccessStatusCode)
{
oneDayWeatherInfo=wait response.Content.ReadAsAsync();
}
}
}
api响应数据中的所有内容都“放”在正确的位置。WeatherDataModel是jsonfile中的C#类结构。看起来像这样的类(第一次阅读时跳过这个):

namespace Blazor_WeatherApi.Models
{
公共类OneDayWeatherDataModel
{
//城市名称
公共字符串名称{get;set;}
公共Main{get;set;}
公共坐标{get;set;}
公共天气列表{get;set;}
公共风{get;set;}
公共云{get;set;}
公共系统系统{get;set;}
公共int可见性{get;set;}
公共int Dt{get;set;}
公共整数时区{get;set;}
公共int Id{get;set;}
公共整数Cod{get;set;}
}
公共班机
{
公共双临时{get;set;}
public double感觉像{get;set;}
公共双临时值{get;set;}
公共双临时最大值{get;set;}
公共int压力{get;set;}
公共属性{get;set;}
}
公共类合作社
{
公共双Lon{get;set;}
公共双Lat{get;set;}
}
公共天气
{
公共int Id{get;set;}
公共字符串Main{get;set;}
公共字符串说明{get;set;}
公共字符串图标{get;set;}
}
公共级风
{
公共双速{get;set;}
公共整数{get;set;}
公共双阵风{get;set;}
}
公共类云
{
公共int All{get;set;}
}
公共类系统
{
公共int类型{get;set;}
公共int Id{get;set;}
公共双消息{get;set;}
公共字符串国家{get;set;}
公共int日出{get;set;}
公共int日落{get;set;}
}
}
现在解决我的问题,我创建了一个blazor项目,可以从根ModelClass调用数据,比如Name(类模型中的第一个属性) 但除了根类之外,我不能使用任何更深一层的类。例如,如果我想获得温度值root->Main->Temp,那么我会得到一个错误。 我在winforms项目中使用了相同的类模型,它是成功的

这里是仅适用于“根类”属性的Blazor代码:

@page”/weather
@注入HttpClient-http;
阿皮卡尔
天气预报
@天气模型。名称
@天气模型
@*
@weatherModel.Main.Temp//如果我取消这3行代码,我会得到错误
*@
@代码{
OneDayWeatherDataModel weatherModel=新的OneDayWeatherDataModel();
专用异步任务GetWeatherData()
{
weatherModel=wait http.GetJsonAsync
("https://api.openweathermap.org/data/2.5/weather?id=...");
}
}
如果我把这3行注释掉,我有一个BlazerAPI调用,我从openweathermap的web api调用中得到了城市的名称。但是

错误发生在我decomment@weatherModel.Main.Temp项目冻结时,Visual Studio告诉我它在HoldModus中 (System.NullReferenceException:“对象引用未设置为对象的实例。”)


这发生在我浏览页面的那一刻,而不是在我点击按钮之后???知道我做错了什么吗?很抱歉,这篇文章太长且相互交织:)

问题在于,您正在以下行中手动实例化该类,并且没有将“Main”属性设置为任何值,因此它为空

OneDayWeatherDataModel weatherModel=新的OneDayWeatherDataModel()

您可以确保使用以下内容填充属性或使用access来避免空引用错误:

    <td>@weatherModel?.Main?.Temp</td>  
@weatherModel?.Main?.Temp

但是,您可能还需要考虑下面的内容,这样,除非得到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=...");
    }
 }
@if(weatherModel!=null){
@天气模型。名称
@天气模型
@weatherModel.Main.Temp
}
@代码{
OneDayWeatherDataModel weatherModel=null;
专用异步任务GetWeatherData()
{
weatherModel=wait http.GetJsonAsync
("https://api.openweathermap.org/data/2.5/weather?id=...");
}
}
非常感谢:)非常好的建议!
    <td>@weatherModel?.Main?.Temp</td>  
@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=...");
    }
 }