【问题标题】:Cannot deserialize the current JSON object Cannot deserialize the current JSON object with group column无法反序列化当前 JSON 对象 无法反序列化当前带有 group 列的 JSON 对象
【发布时间】:2020-07-14 21:53:35
【问题描述】:

我正在调用 API 并尝试将 Jsonresult 存储到 IEnumerable 模型类中。 json 结果具有组标题列consolidated_weather。 当我运行程序时出现以下错误

JsonSerializationException:无法将当前 JSON 对象反序列化为“System.Collections.Generic.IEnumerable”类型。

避免json结果中的组头后,如何将给定的json结果调用到IEnumerable模型类中。

我正在使用以下两种模型。

 public class Weather
{
    [Key]
    public long id { get; set; }
    public string weather_state_name { get; set; }
    public string weather_state_abbr { get; set; }
    public string wind_direction_compass { get; set; }
    public DateTime created { get; set; }
    public DateTime applicable_date { get; set; }
    public decimal min_temp { get; set; }
    public decimal max_temp { get; set; }
    public decimal the_temp { get; set; }
    public double wind_speed { get; set; }
    public decimal wind_direction { get; set; }
    public decimal air_pressure { get; set; }
    public decimal Humidity { get; set; }
    public string  Visibllity { get; set; }
    public decimal Predictability { get; set; } 
   }
 public class WeatherList
{
    public IEnumerable<Weather> consolidated_weather { get; set; }
}
  public async Task<IEnumerable<Weather>> GetWeatherAsync(string woied)
    {

        var url = SD.APIBaseUrl + woied;
        var request = new HttpRequestMessage(HttpMethod.Get, url);
        var client = _clientFactory.CreateClient();
        HttpResponseMessage response = await client.SendAsync(request);
       
        IEnumerable<Weather> weather = new List<Weather>();
        WeatherList weatherList = new WeatherList();
        
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);
            return weatherList;
        }
        return null;
    }

API 结果如下

<!-- language: lang-html -->

    {
        "consolidated_weather": [
            {
                "id": 4577625064341504,
                "weather_state_name": "Heavy Rain",
                "weather_state_abbr": "hr",
                "wind_direction_compass": "WSW",
                "created": "2020-07-14T19:35:14.577740Z",
                "applicable_date": "2020-07-14",
                "min_temp": 11.11,
                "max_temp": 15.05,
                "the_temp": 14.32,
                "wind_speed": 6.570953330777592,
                "wind_direction": 254.13274105640758,
                "air_pressure": 1016.5,
                "humidity": 85,
                "visibility": 7.654361031575599,
                "predictability": 77
            },
            {
                "id": 4896540210495488,
                "weather_state_name": "Showers",
                "weather_state_abbr": "s",
                "wind_direction_compass": "WNW",
                "created": "2020-07-14T19:35:17.569534Z",
                "applicable_date": "2020-07-15",
                "min_temp": 12.31,
                "max_temp": 17.03,
                "the_temp": 16.509999999999998,
                "wind_speed": 7.600821124862802,
                "wind_direction": 284.49357944800784,
                "air_pressure": 1015.5,
                "humidity": 82,
                "visibility": 13.558008729022509,
                "predictability": 73
            },
]
"title": "Texas",
    "location_type": "City", 
    "timezone": "US"
<!-- end snippet -->

【问题讨论】:

  • 您好,我认为“null”可能与您的 jsonString 有关。你先贴的jsonString没有完成。你是故意缩短它还是整个字符串?
  • 我建议您可以先从我的代码中测试 jsonString 以反序列化 WeatherList 对象作为我们的方式。如果像我们一样成功,那么您可以检查 jsonString API。
  • 代码存在多个问题。 JSON 字符串被截断,该方法返回 IEnumerable&lt;Weather&gt;,但代码返回单个 WeatherList,并且从未使用过 weather 变量。这甚至不会编译,更不用说产生异常

标签: asp.net-core asp.net-web-api


【解决方案1】:

从您的 json 字符串中,我们可以看到对象 consolidated_weather 中有一个 weather 对象列表。 所以需要将json解析成WeatherList。

public class WeatherList {
        public IEnumerable<Weather> consolidated_weather { get; set; }
    }


[HttpPost("/weather")]
public async Task<IEnumerable<Weather>> GetWeatherAsync()
    {

        string jsonString = "{\"consolidated_weather\":[{\"id\":4577625064341504,\"weather_state_name\":\"Heavy Rain\",\"weather_state_abbr\":\"hr\",\"wind_direction_compass\":\"WSW\",\"created\":\"2020-07-14T19:35:14.577740Z\",\"applicable_date\":\"2020-07-14\",\"min_temp\":11.11,\"max_temp\":15.05,\"the_temp\":14.32,\"wind_speed\":6.570953330777592,\"wind_direction\":254.13274105640758,\"air_pressure\":1016.5,\"humidity\":85,\"visibility\":7.654361031575599,\"predictability\":77},{\"id\":4896540210495488,\"weather_state_name\":\"Showers\",\"weather_state_abbr\":\"s\",\"wind_direction_compass\":\"WNW\",\"created\":\"2020-07-14T19:35:17.569534Z\",\"applicable_date\":\"2020-07-15\",\"min_temp\":12.31,\"max_temp\":17.03,\"the_temp\":16.509999999999998,\"wind_speed\":7.600821124862802,\"wind_direction\":284.49357944800784,\"air_pressure\":1015.5,\"humidity\":82,\"visibility\":13.558008729022509,\"predictability\":73}]}";
        IEnumerable<Weather> weather = new List<Weather>();


        WeatherList weatherList = new WeatherList();
        weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);

        return weatherList.consolidated_weather;

    }

测试结果

更新


 public class WeatherList 
 {
    public IEnumerable<Weather> consolidated_weather { get; set; }

    public string title { get; set; }
    public string location_type { get; set; }
    public string timezone { get; set; }
    
 }

您的json 字符串中有两个缺失:

<!-- language: lang-html -->

    {
        "consolidated_weather": [
            {
                "id": 4577625064341504,
                ...
            },
            {
                "id": 4896540210495488,
                ...
            },
] <!-- missing , -->
"title": "Texas",
    "location_type": "City", 
    "timezone": "US"
<!-- missing } -->

<!-- end snippet -->

【讨论】:

  • 我做了同样的事情,但仍然没有工作。错误消息即将出现“无法隐式转换类型 system.collections.Generic.Ienumerable.
  • 嗨,你能更新你的代码吗?这样我就可以帮忙了。
  • Michelle 我忘了添加 'Title' 'Location Type' 该列应该从字符串中被忽略。请你看看我更新的帖子
  • 这就是你没有很好地反序列化 Object 的原因。它必须与json格式相同。将其他属性添加到 WeatherList 中,它就可以正常工作了。
  • 从未使用过weather 变量
【解决方案2】:

假设您无法更改 Json 的内容,您可以创建一个包含天气列表的 ConsolidatedWeather 类

public class ConsolidatedWeather
{
    public List<Weather> Consolidated_Weather { get; set; }
}

在你的反序列化部分

return JsonConvert.DeserializeObject<ConsolidatedWeather>(jsonString).Consolidated_Weather;

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2017-03-06
    • 2017-10-28
    相关资源
    最近更新 更多