ASP.NET MVC 核心消费 Web API 与数组?
ASP.NET MVC Core Consuming Web API with Arrays?
我遇到了困难,虽然我彻底(我认为)研究了网络,包括在 SO 处,但我似乎找不到我要找的东西——或者我很迟钝。我的环境是.NET6,ASP.NET Core MVC.
我从 API 中成功获取 Json 后遇到的错误是:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsumeWebApi.Models.Weather+Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'region', line 1, position 10.
这对我来说似乎很简单,所以我检查了返回的字符串。
{
"region": "My Town, ST",
"currentConditions": {
"dayhour": "Tuesday 9:00 AM",
"temp": {
"c": 14,
"f": 58
},
"precip": "15%",
"humidity": "72%",
"wind": {
"km": 3,
"mile": 2
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/64/sunny.png",
"comment": "Sunny"
},
"next_days": [
{
"day": "Tuesday",
"comment": "Scattered showers",
"max_temp": {
"c": 24,
"f": 75
},
"min_temp": {
"c": 14,
"f": 58
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_s_cloudy.png"
},
{
"day": "Wednesday",
"comment": "Scattered showers",
"max_temp": {
"c": 17,
"f": 62
},
"min_temp": {
"c": 9,
"f": 49
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_s_cloudy.png"
},
{
"day": "Thursday",
"comment": "Mostly cloudy",
"max_temp": {
"c": 22,
"f": 71
},
"min_temp": {
"c": 12,
"f": 54
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Friday",
"comment": "Rain",
"max_temp": {
"c": 17,
"f": 62
},
"min_temp": {
"c": 12,
"f": 53
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain.png"
},
{
"day": "Saturday",
"comment": "Showers",
"max_temp": {
"c": 16,
"f": 60
},
"min_temp": {
"c": 8,
"f": 46
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_light.png"
},
{
"day": "Sunday",
"comment": "Partly cloudy",
"max_temp": {
"c": 19,
"f": 66
},
"min_temp": {
"c": 7,
"f": 45
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Monday",
"comment": "Partly cloudy",
"max_temp": {
"c": 22,
"f": 72
},
"min_temp": {
"c": 9,
"f": 48
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Tuesday",
"comment": "Mostly sunny",
"max_temp": {
"c": 26,
"f": 79
},
"min_temp": {
"c": 13,
"f": 55
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
}
],
"contact_author": {
"email": "communication.with.users@gmail.com",
"auth_note": "Mail me for feature requests, improvement, bug, help, ect... Please tell me if you want me to provide any other free easy-to-use API services"
},
"data_source": "https://www.google.com/search?lr=lang_en&q=weather+in+mycity+state"
}
我看到的唯一数组是“next_days”,但正如您在下面的数据模型中看到的那样,我考虑到了这一点。我的模型是由 Visual Studio 中的“粘贴 Json 为 类”功能创建的,是:
namespace ConsumeWebApi.Models
{
public class Weather
{
public class Root
{
public string? Region { get; set; }
public Currentconditions? CurrentConditions { get; set; }
public Next_Days[]? Next_days { get; set; }
public Contact_Author? Contact_author { get; set; }
public string? Data_source { get; set; }
}
public class Currentconditions
{
public string? Dayhour { get; set; }
public Temp? Temp { get; set; }
public string? Precip { get; set; }
public string? Humidity { get; set; }
public Wind? Wind { get; set; }
public string? IconURL { get; set; }
public string? Comment { get; set; }
}
public class Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
public class Wind
{
public int? Km { get; set; }
public int? Mile { get; set; }
}
public class Contact_Author
{
public string? Email { get; set; }
public string? Auth_note { get; set; }
}
public class Next_Days
{
public string? Day { get; set; }
public string? Comment { get; set; }
public Max_Temp? Max_temp { get; set; }
public Min_Temp? Min_temp { get; set; }
public string? IconURL { get; set; }
}
public class Max_Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
public class Min_Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
}
}
让我们看看我的控制器。 (这是我怀疑我不太明白的地方。这里或我的模型。)
public async Task<IActionResult> Index()
{
List<Weather.Root> weatherInfo = new List<Weather.Root>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://weatherdbi.herokuapp.com/data/weather/mycity+state"))
{
string? apiResponse = await response.Content.ReadAsStringAsync();
weatherInfo = JsonConvert.DeserializeObject<List<Weather.Root>>(apiResponse);
}
}
return View(weatherInfo);
}
Visual Studio 中的调试显示 apiResponse 具有我在上面发布的 Json 数据 - 找不到额外的方括号 []。
对于这个粗略的问题,我深表歉意,并提前感谢您的回复。我是 C# 的新手,但不是 Web 开发的新手。
根是对象,不是集合。所以只使用这个代码
weatherInfo = JsonConvert.DeserializeObject<Weather.Root>(apiResponse);
解决“Cannot deserialize the current JSON array (e.g. [1,2,3])”
按照以下步骤操作:
- 使用在线转换器将 JSON 转换为 C#(例如 https://json2csharp.com/)。
- 将您的 JSON 粘贴到 JSON 部分并生成 C#。
- 它将生成代码并为您提供反序列化的方法。
例如JsonDataService myData = JsonConvert.DeserializeObject(JSON); //JSON 解析
我遇到了困难,虽然我彻底(我认为)研究了网络,包括在 SO 处,但我似乎找不到我要找的东西——或者我很迟钝。我的环境是.NET6,ASP.NET Core MVC.
我从 API 中成功获取 Json 后遇到的错误是:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsumeWebApi.Models.Weather+Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'region', line 1, position 10.
这对我来说似乎很简单,所以我检查了返回的字符串。
{
"region": "My Town, ST",
"currentConditions": {
"dayhour": "Tuesday 9:00 AM",
"temp": {
"c": 14,
"f": 58
},
"precip": "15%",
"humidity": "72%",
"wind": {
"km": 3,
"mile": 2
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/64/sunny.png",
"comment": "Sunny"
},
"next_days": [
{
"day": "Tuesday",
"comment": "Scattered showers",
"max_temp": {
"c": 24,
"f": 75
},
"min_temp": {
"c": 14,
"f": 58
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_s_cloudy.png"
},
{
"day": "Wednesday",
"comment": "Scattered showers",
"max_temp": {
"c": 17,
"f": 62
},
"min_temp": {
"c": 9,
"f": 49
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_s_cloudy.png"
},
{
"day": "Thursday",
"comment": "Mostly cloudy",
"max_temp": {
"c": 22,
"f": 71
},
"min_temp": {
"c": 12,
"f": 54
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Friday",
"comment": "Rain",
"max_temp": {
"c": 17,
"f": 62
},
"min_temp": {
"c": 12,
"f": 53
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain.png"
},
{
"day": "Saturday",
"comment": "Showers",
"max_temp": {
"c": 16,
"f": 60
},
"min_temp": {
"c": 8,
"f": 46
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/rain_light.png"
},
{
"day": "Sunday",
"comment": "Partly cloudy",
"max_temp": {
"c": 19,
"f": 66
},
"min_temp": {
"c": 7,
"f": 45
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Monday",
"comment": "Partly cloudy",
"max_temp": {
"c": 22,
"f": 72
},
"min_temp": {
"c": 9,
"f": 48
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
},
{
"day": "Tuesday",
"comment": "Mostly sunny",
"max_temp": {
"c": 26,
"f": 79
},
"min_temp": {
"c": 13,
"f": 55
},
"iconURL": "https://ssl.gstatic.com/onebox/weather/48/partly_cloudy.png"
}
],
"contact_author": {
"email": "communication.with.users@gmail.com",
"auth_note": "Mail me for feature requests, improvement, bug, help, ect... Please tell me if you want me to provide any other free easy-to-use API services"
},
"data_source": "https://www.google.com/search?lr=lang_en&q=weather+in+mycity+state"
}
我看到的唯一数组是“next_days”,但正如您在下面的数据模型中看到的那样,我考虑到了这一点。我的模型是由 Visual Studio 中的“粘贴 Json 为 类”功能创建的,是:
namespace ConsumeWebApi.Models
{
public class Weather
{
public class Root
{
public string? Region { get; set; }
public Currentconditions? CurrentConditions { get; set; }
public Next_Days[]? Next_days { get; set; }
public Contact_Author? Contact_author { get; set; }
public string? Data_source { get; set; }
}
public class Currentconditions
{
public string? Dayhour { get; set; }
public Temp? Temp { get; set; }
public string? Precip { get; set; }
public string? Humidity { get; set; }
public Wind? Wind { get; set; }
public string? IconURL { get; set; }
public string? Comment { get; set; }
}
public class Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
public class Wind
{
public int? Km { get; set; }
public int? Mile { get; set; }
}
public class Contact_Author
{
public string? Email { get; set; }
public string? Auth_note { get; set; }
}
public class Next_Days
{
public string? Day { get; set; }
public string? Comment { get; set; }
public Max_Temp? Max_temp { get; set; }
public Min_Temp? Min_temp { get; set; }
public string? IconURL { get; set; }
}
public class Max_Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
public class Min_Temp
{
public int? C { get; set; }
public int? F { get; set; }
}
}
}
让我们看看我的控制器。 (这是我怀疑我不太明白的地方。这里或我的模型。)
public async Task<IActionResult> Index()
{
List<Weather.Root> weatherInfo = new List<Weather.Root>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://weatherdbi.herokuapp.com/data/weather/mycity+state"))
{
string? apiResponse = await response.Content.ReadAsStringAsync();
weatherInfo = JsonConvert.DeserializeObject<List<Weather.Root>>(apiResponse);
}
}
return View(weatherInfo);
}
Visual Studio 中的调试显示 apiResponse 具有我在上面发布的 Json 数据 - 找不到额外的方括号 []。
对于这个粗略的问题,我深表歉意,并提前感谢您的回复。我是 C# 的新手,但不是 Web 开发的新手。
根是对象,不是集合。所以只使用这个代码
weatherInfo = JsonConvert.DeserializeObject<Weather.Root>(apiResponse);
解决“Cannot deserialize the current JSON array (e.g. [1,2,3])” 按照以下步骤操作:
- 使用在线转换器将 JSON 转换为 C#(例如 https://json2csharp.com/)。
- 将您的 JSON 粘贴到 JSON 部分并生成 C#。
- 它将生成代码并为您提供反序列化的方法。 例如JsonDataService myData = JsonConvert.DeserializeObject(JSON); //JSON 解析