C#.net http |如何从API中获取对象层

C#.net http |如何从API中获取对象层,c#,json.net,C#,Json.net,我学习了如何使用microsoft文档从API获取信息,microsoft文档没有显示如何获取嵌套/深层对象。我发现的唯一一个视频展示了如何做,它是这样做的。但是,我无法让它工作,只收到一个错误,说明: "Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Dashboard.Weather' because the typ

我学习了如何使用microsoft文档从API获取信息,microsoft文档没有显示如何获取嵌套/深层对象。我发现的唯一一个视频展示了如何做,它是这样做的。但是,我无法让它工作,只收到一个错误,说明:

"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Dashboard.Weather' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
感谢您的帮助,我正在尝试获取“Weather.description”,以下是我的示例代码:

public class Weather
{
    public string Description{ get; set; }
}

public class Product
{
    public Weather Weather { get; set; }
}

public static class ApiHelper
{
    static string city_id = "CITY_ID";
    static string api_key = "API_KEY";
    public static HttpClient client = new HttpClient();

    public static void InitializeClient()
    {
        client.BaseAddress = new Uri($"http://api.openweathermap.org/data/2.5/weather?id={city_id}&APPID={api_key}");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public static async Task<Weather> GetProductAsync()
    {
        Product product = null;
        HttpResponseMessage response = await client.GetAsync("");
        if (response.IsSuccessStatusCode)
        {
            product = await response.Content.ReadAsAsync<Product>();
        }
        return product.Weather;

    }
}

    async void SetLabelText()
    {
        var weather = await ApiHelper.GetProductAsync();

        descriptionLabel.Text = $"Description: {weather.Description}";
    }

您的
产品
模型与您收到的json不正确对齐

您发布的json将天气作为一个列表,但
Product
假定它只是一个对象。因此,当看到json解析器是实际json中的数组而不是json对象时,它会正确地失败


解决方法应该简单<代码>产品。天气应为<代码>列表(或<代码>IEnumerable或<代码>天气[],以适合您的需要为准)。

您的<代码>产品型号与您收到的json不正确对齐

您发布的json将天气作为一个列表,但
Product
假定它只是一个对象。因此,当看到json解析器是实际json中的数组而不是json对象时,它会正确地失败


解决方法应该简单
Product.Weather
应为
List
类型(或
IEnumerable
Weather[]
,以适合您的需要为准)。

这不是有效的json,很可能是您最初的问题。这真的是api的响应吗?baxorr你能发布产品模型吗?根据错误,您的产品模型可能没有假设您的天气是一个列表。@BillKeller发布了
产品
模型,而
天气
不是一个列表,但由于json无效,我们不知道它是否应该是一个列表。为给定的纬度和经度收集天气项目有意义吗?也许,我不熟悉那个api。如果他们需要api密钥而不使用TLS,我永远不会。啊,错过了这一点,好吧,即使json是正确的,天气也需要在产品模型上列出。@Crowcoder我很抱歉,我不知道我是否发布了错误的片段。我已经添加了API的完整无格式响应。我想得到的是天气预报中的描述,它不是有效的json,这很可能是您最初的问题。这真的是api的响应吗?baxorr你能发布产品模型吗?根据错误,您的产品模型可能没有假设您的天气是一个列表。@BillKeller发布了
产品
模型,而
天气
不是一个列表,但由于json无效,我们不知道它是否应该是一个列表。为给定的纬度和经度收集天气项目有意义吗?也许,我不熟悉那个api。如果他们需要api密钥而不使用TLS,我永远不会。啊,错过了这一点,好吧,即使json是正确的,天气也需要在产品模型上列出。@Crowcoder我很抱歉,我不知道我是否发布了错误的片段。我已经添加了API的完整无格式响应。我想得到的是weatherWow中的描述,真的很简单!谢谢比尔,我希望你有一个伟大的一天!哇,真是那么简单!谢谢比尔,我希望你有一个伟大的一天!
{"coord":{"lon":-89.59,"lat":41.56},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":279.27,"feels_like":275.4,"temp_min":278.15,"temp_max":280.37,"pressure":1027,"humidity":74},"visibility":16093,"wind":{"speed":3.1,"deg":200},"clouds":{"all":1},"dt":1576951484,"sys":{"type":1,"id":3561,"country":"US","sunrise":1576934493,"sunset":1576967469},"timezone":-21600,"id":4915397,"name":"Walnut","cod":200}