繁体   English   中英

ASP.NET MVC 核心消耗 Web API 与 Arrays?

[英]ASP.NET MVC Core Consuming Web API with Arrays?

我遇到了困难,虽然我彻底(我认为)研究了 web,包括在 SO 处,但我似乎找不到我要找的东西——或者我很迟钝。 我的环境是.NET6,ASP.NET Core MVC。

从我的 API 成功获取 Json 后,我遇到的错误是:

JsonSerializationException:无法将当前的 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[ConsumeWebApi.Models.Weather+Root]' 因为该类型需要一个 JSON 数组(例如 [1,2,3]) 以正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为正常的 .NET 类型(例如,不是像 integer 这样的原始类型,不是像数组或列表),可以从 JSON object 反序列化。也可以将 JsonObjectAttribute 添加到类型以强制它从 JSON object 反序列化。路径“区域”,第 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”,但正如您在下面的数据 model 中看到的那样,我对此进行了说明。 我的 model 是由 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; }
        }


    }
}

让我们看看我的 controller。(这是我怀疑我不太明白的地方。这里或我的 model。)

        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 开发的新手。

Root是一个object,不是集合。 所以只使用这段代码

 weatherInfo = JsonConvert.DeserializeObject<Weather.Root>(apiResponse);

要解决“无法反序列化当前 JSON 数组(例如 [1,2,3])”,请按照以下步骤操作:

  1. 使用在线转换器将 JSON 转换为 C#(例如https://json2csharp.com/ )。
  2. 将您的 JSON 粘贴到 JSON 部分并生成 C#。
  3. 它将生成代码并为您提供反序列化的方法。 例如 JsonDataService myData = JsonConvert.DeserializeObject(JSON); //JSON解析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM