繁体   English   中英

无法反序列化当前 JSON.Net 数组 C#

[英]Cannot deserialize the current JSON.Net array C#

我对编程很陌生,我还有很多东西要学,我需要一点帮助:)!

我在其他帖子中看到了同样的错误,但即使有这些解释我也无法解决任何问题

我得到的错误

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'JSON_TEST2.Class1+weatherinfo' because the type requires a JSON object (eg {"name":"value"} ) 以正确反序列化。 要修复此错误,请将 JSON 更改为 JSON object (例如 {"name":"value"})或将实现的反序列化类型接口更改为数组或集合从 JSON 阵列反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。 路径“天气”,第 1 行,position 45。

我有这个 class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace JSON_TEST2
{
    public class Class1
    {
        public class coord
        {
            public double lat { get; set; }
            public double lon { get; set; }
        }

        public class weatherinfo
        {
            public string[] weather { get; set; }
        }

        public class WeatherMain
        {
           public coord coord { get; set; }
           public weatherinfo weather { get; set; }

            public void display()
            {
                Console.WriteLine("lon: {0}", this.coord.lon);
                Console.WriteLine("lat: {0}", this.coord.lat);
                Console.WriteLine("id: {0}", this.weather.weather);
            }
        }     
    }
}

我用这个反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net;

namespace JSON_TEST2
{
    class Program
    {
        static void Main()
        {
            WebClient wc = new WebClient();
            var json = wc.DownloadString(@"http://api.openweathermap.org/data/2.5/weather?q=Bucharest,ro&APPID=829b7bfc0558b9e501f43fc6087fca3a");
            Console.WriteLine(json);
            Class1.WeatherMain vreme = JsonConvert.DeserializeObject <Class1.WeatherMain>(json);
            vreme.display();
            Console.ReadLine();
        }
    }
}

这是我从服务器获得的 JSON:

{
   "coord":{
      "lon":26.1,
      "lat":44.44
   },
   "weather":[
      {
         "id":804,
         "main":"Clouds",
         "description":"overcast clouds",
         "icon":"04n"
      }
   ],
   "base":"stations",
   "main":{
      "temp":287.64,
      "pressure":1012,
      "humidity":87,
      "temp_min":287.15,
      "temp_max":288.15
   },
   "visibility":8000,
   "wind":{
      "speed":2.6,
      "deg":50
   },
   "clouds":{
      "all":100
   },
   "dt":1573682313,
   "sys":{
      "type":1,
      "id":6911,
      "country":"RO",
      "sunrise":1573708189,
      "sunset":1573743018
   },
   "timezone":7200,
   "id":683506,
   "name":"Bucharest",
   "cod":200
}

如评论中所述,您的类与 Json 不匹配。这是因为天气本身是一个数组,而不是 object。

"weather":[
          {
             "id":804,
             "main":"Clouds",
             "description":"overcast clouds",
             "icon":"04n"
          }
       ]

您需要更改 weatherinfo 和 WeatherMain 如下。

public class weatherinfo
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class WeatherMain
{
   public coord coord { get; set; }
   public List<weatherinfo> weather { get; set; }

    public void display()
    {
        Console.WriteLine("lon: {0}", this.coord.lon);
        Console.WriteLine("lat: {0}", this.coord.lat);
        Console.WriteLine("id: {0}", string.Join(Environment.NewLine,this.weather.Select(c=>$"Weather:{c.main},Description:{c.description}")));
    }
} 

演示代码

暂无
暂无

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

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