使用 API 会产生错误"An object reference is required for the nonstatic field, method, or property 'member'



作为一个初学者,我仍然不完全明白我在做什么。我试图添加一个天气API到一个网页,但当我试图调用视图上的类,它不起作用,我得到一个错误"一个对象引用是非静态字段,方法,或属性' weatheragent . current . temperature '">

模型
using Newtonsoft.Json;
namespace Exercice.Models
{
public class WeatherApi
{
//Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Current
{
[JsonProperty("observation_time")]
public string ObservationTime { get; set; }
[JsonProperty("temperature")]
public int Temperature { get; set; }
[JsonProperty("weather_code")]
public int WeatherCode { get; set; }
[JsonProperty("weather_icons")]
public List<string> WeatherIcons { get; set; }
[JsonProperty("weather_descriptions")]
public List<string> WeatherDescriptions { get; set; }
[JsonProperty("wind_speed")]
public int WindSpeed { get; set; }
[JsonProperty("wind_degree")]
public int WindDegree { get; set; }
[JsonProperty("wind_dir")]
public string WindDir { get; set; }
[JsonProperty("pressure")]
public int Pressure { get; set; }
[JsonProperty("precip")]
public int Precip { get; set; }
[JsonProperty("humidity")]
public int Humidity { get; set; }
[JsonProperty("cloudcover")]
public int Cloudcover { get; set; }
[JsonProperty("feelslike")]
public int Feelslike { get; set; }
[JsonProperty("uv_index")]
public int UvIndex { get; set; }
[JsonProperty("visibility")]
public int Visibility { get; set; }
[JsonProperty("is_day")]
public string IsDay { get; set; }
}
public class Location
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("region")]
public string Region { get; set; }
[JsonProperty("lat")]
public string Lat { get; set; }
[JsonProperty("lon")]
public string Lon { get; set; }
[JsonProperty("timezone_id")]
public string TimezoneId { get; set; }
[JsonProperty("localtime")]
public string Localtime { get; set; }
[JsonProperty("localtime_epoch")]
public int LocaltimeEpoch { get; set; }
[JsonProperty("utc_offset")]
public string UtcOffset { get; set; }
}
public class Request
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("query")]
public string Query { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("unit")]
public string Unit { get; set; }
}
public class Root
{
[JsonProperty("request")]
public Request Request { get; set; }
[JsonProperty("location")]
public Location Location { get; set; }
[JsonProperty("current")]
public Current Current { get; set; }
}
}
}

控制器

public async Task<IActionResult> Weather()
{
string baseAddress = "http://api.weatherstack.com/";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
string path = "current?access_key=<MYACCESSKEY>&query=Madrid";
HttpResponseMessage response = client.GetAsync(path).Result;
string myJsonResponse = await response.Content.ReadAsStringAsync();
WeatherApiResponse apiResponse =
JsonConvert.DeserializeObject<WeatherApiResponse>(myJsonResponse);
return View(apiResponse);
}

视图

@model WeatherApi
<p>Temperature @Model.Current.Temperature&deg;</p>

当你试图在一个对象上引用一个属性或函数时,它是空的,或者在它被初始化为任何东西之前。

基本上,你要寻找"。"然后确保一切之前的";是零。

在下面的这行中,它看起来像是Model或Model。当前值可能为空

Model.Current.Temperature

你可以尝试"protect">

@if (@Model != null && @Model.Current != null)
{
<p>Temperature @Model.Current.Temperature&deg;</p> 
}

但是你真的必须调试c#代码,找出什么是空的,为什么是空的。这可能是API返回的内容有问题。

您的WeatherApi定义有问题。与其把类定义放在里面,不如这样定义模型:

public class WeatherApi 
{
public Current Current { get; set; }
// rest of properties...
}

除此之外,如果我没记错的话,你不需要在JSON中为名字定义属性,除非你想让name属性与代码中的不同。

也让用@model代替@Model,因为你在模型定义中使用了小的第一个字母:

<p>Temperature @model.Current.Temperature&deg;</p>

相关内容

  • 没有找到相关文章

最新更新