【问题标题】:Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {"name":"value"})无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 '',因为该类型需要 JSON 对象(例如 {"name":"value"})
【发布时间】:2016-12-20 20:56:39
【问题描述】:

我明白了

“无法将当前的JSON数组(如[1,2,3])反序列化为类型'',因为该类型需要一个JSON对象(如{“name”:“value”})才能正确反序列化”错误

我浏览了大多数类似的问题,但没有找到我要寻找的答案,所以我要问一个新问题。

这是我的 JSON:

{
    "coord": {
        "lon": 105.84,
        "lat": 21.59
    },
    "weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 20.31,
        "pressure": 1010.36,
        "humidity": 98,
        "temp_min": 20.31,
        "temp_max": 20.31,
        "sea_level": 1026.71,
        "grnd_level": 1010.36
    },
    "wind": {
        "speed": 1.86,
        "deg": 124.5
    },
    "rain": {
        "3h": 0.3075
    },
    "clouds": {
        "all": 92
    },
    "dt": 1482264413,
    "sys": {
        "message": 0.0114,
        "country": "VN",
        "sunrise": 1482190209,
        "sunset": 1482229157
    },
    "id": 1566319,
    "name": "Thai Nguyen",
    "cod": 200
}

这是我的代码:

 private void Form1_Load(object sender, EventArgs e)
 {
     string url = "http://api.openweathermap.org/data/2.5/weather?q=London&units=metric&appid={MyAppID}";
     HttpWebRequest httpWebRequset = (HttpWebRequest)WebRequest.Create(url);
     httpWebRequset.Method = WebRequestMethods.Http.Get;
     httpWebRequset.ContentType = "application/json";

     HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequset.GetResponse();

     using (var StreamReader = new StreamReader(httpResponse.GetResponseStream()))
     {
         string responseString = StreamReader.ReadToEnd();

         ResponseData data = JsonConvert.DeserializeObject<ResponseData>(responseString);

         ShowTemp.Text = data.main.temp + "°C";
         ShowWheater.Text = data.weather.description;
    }
}

当我尝试获取温度时,我可以找到它,但是当我想从以下位置获取描述时:

{
    ...
    "weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }
    ]
    ...
}

我得到了错误。

【问题讨论】:

  • 显示您的ResponseData 课程。
  • ResponseData 类是什么样的?我怀疑它需要一个格式为'{“id”:500,“main”:“Rain”,“description”:“light rain”,“icon”:“10n”}'的天气对象。您尝试解析为天气对象的方括号表示它正在尝试读取对象数组。
  • 确实,在此类的根对象中,您可能需要类似public List&lt;Weather&gt; weather {get;set;} 的内容,其中Weather 是具有iddescription 等属性的类。
  • 这是我的 responseData 类 ResponseData { public Main main;公共天气天气; } 类主 { 公共字符串临时; } 公共类天气 { 公共字符串描述; }

标签: c# json json.net


【解决方案1】:

JSON 包含一个 Weather 数组,即使它只有一个条目。这由方括号表示,见下文:

“天气”:[ { “身份证”:500, “主”:“雨”, “描述”:“小雨”, “图标”:“10n” } ]

你说这是你的ResponseData类:

class ResponseData 
{ 
    public Main main; 
    public Weather weather; 
} 

class Main 
{ 
    public string temp; 
} 

class Weather 
{ 
    public string description; 
}

ResponseData 类更改为:

public class ResponseData 
{ 
    public Main main { get; set; } 
    public List<Weather> weather { get; set; } // This is a List<T> of Weather
}                                              // It can contain more than one entry 
                                               // for weather

public class Main 
{ 
    public double temp { get; set; } // This is a double
} 

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

您还必须在项目中添加对System.Collections 的引用以及相关的using

using System.Collections;

由于天气现在是一个列表,您必须按如下索引访问它:

ShowWeather.Text = data.weather[0].description;

【讨论】:

  • 现在我收到此错误“列表”不包含“描述”的定义,并且找不到接受“列表”类型的第一个参数的扩展方法“描述” (您是否缺少 using 指令或程序集引用?)
  • 我的错,我只是忘记了获取/设置
  • IDE 中是否有任何代码用红色下划线表示错误?听起来您可能缺少对包含 List&lt;T&gt; 的库的引用。添加对 System.Collections 的引用并将 using 放在代码的顶部。我将编辑我的答案。
  • 我的代码在 ShowWheater.Text = data.weather 所在的行下有红色。 描述;
  • 您可能想要ShowWeather.Text = data.weather[0].description;,因为 Weather 现在是一个包含一个对象的列表,因此您必须按索引访问第一个条目。
猜你喜欢
  • 2020-07-03
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多