返回

c#-ASP.NET MVC Core 使用带有数组的 Web API?

发布时间:2022-05-08 04:32:58 232
# ruby

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

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

JsonSerializationException:无法反序列化当前的JSON对象(例如,{“名称”:“值”})进入“类型”系统。收藏。通用的列出“1[ConsumerWebAPI.Models.Weather+Root]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化的类型,使其成为正常类型。NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径“区域”,第1行,位置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://img.hake.cc/other_net_images/other_images_skf/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://img.hake.cc/other_net_images/other_images_skf/rain_s_cloudy.png"
        },
        {
            "day": "Wednesday",
            "comment": "Scattered showers",
            "max_temp": {
                "c": 17,
                "f": 62
            },
            "min_temp": {
                "c": 9,
                "f": 49
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/rain_s_cloudy.png"
        },
        {
            "day": "Thursday",
            "comment": "Mostly cloudy",
            "max_temp": {
                "c": 22,
                "f": 71
            },
            "min_temp": {
                "c": 12,
                "f": 54
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/partly_cloudy.png"
        },
        {
            "day": "Friday",
            "comment": "Rain",
            "max_temp": {
                "c": 17,
                "f": 62
            },
            "min_temp": {
                "c": 12,
                "f": 53
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/rain.png"
        },
        {
            "day": "Saturday",
            "comment": "Showers",
            "max_temp": {
                "c": 16,
                "f": 60
            },
            "min_temp": {
                "c": 8,
                "f": 46
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/rain_light.png"
        },
        {
            "day": "Sunday",
            "comment": "Partly cloudy",
            "max_temp": {
                "c": 19,
                "f": 66
            },
            "min_temp": {
                "c": 7,
                "f": 45
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/partly_cloudy.png"
        },
        {
            "day": "Monday",
            "comment": "Partly cloudy",
            "max_temp": {
                "c": 22,
                "f": 72
            },
            "min_temp": {
                "c": 9,
                "f": 48
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/partly_cloudy.png"
        },
        {
            "day": "Tuesday",
            "comment": "Mostly sunny",
            "max_temp": {
                "c": 26,
                "f": 79
            },
            "min_temp": {
                "c": 13,
                "f": 55
            },
            "iconURL": "https://img.hake.cc/other_net_images/other_images_skf/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"
}

我看到的唯一数组是“for”;“接下来的几天”;但正如你在我下面的数据模型中看到的,我解释了这一点。我的模型是由;“将Json粘贴为类”;Visual Studio中的功能是:

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 Index()
        {
            List weatherInfo = new List();
            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>(apiResponse);
                }
            }
            return View(weatherInfo);
        }

在VisualStudio中进行调试时,apiResponse显示了我在上面发布的Json数据,没有额外的方括号[]。

我为这个基本问题道歉,并提前感谢您的回答。我是C#新手,但对web开发并不陌生。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像