[英]Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”})
I got 我有
"Cannot deserialize the current JSON array (eg [1,2,3]) into type '' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly" Error
“无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”})才能正确地反序列化”
I looked through most of the similar questions but found no answer to what I'm looking for, so I'm asking a new one. 我浏览了大多数类似的问题,但没有找到想要的答案,所以我要提出一个新的问题。
Here is my JSON: 这是我的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
}
And here is my code: 这是我的代码:
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;
}
}
When ever I try to get a temperature I can find it, but when I want to get the description from: 每当我尝试获取温度时,都可以找到它,但是当我想要从以下位置获取描述时:
{
...
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
]
...
}
I get the error. 我得到了错误。
The JSON contains an array of Weather
, even if it only has one entry. JSON包含一个
Weather
数组,即使它只有一个条目也是如此。 This is denoted by the square brackets, see below: 用方括号表示,请参见下文:
"weather" : [ { "id":500, "main":"Rain", "description":"light rain", "icon":"10n" } ]
“天气”: [ {“ id”:500,“ main”:“雨”,“ description”:“小雨”,“ icon”:“ 10n”} ]
You said this was your ResponseData
class: 您说这是您的
ResponseData
类:
class ResponseData
{
public Main main;
public Weather weather;
}
class Main
{
public string temp;
}
class Weather
{
public string description;
}
Change the ResponseData
class to this: 将
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; }
}
You must also have a reference to System.Collections
added to your project and the relevant using
: 您还必须引用添加到您的项目中的
System.Collections
以及相关的using
:
using System.Collections;
As the Weather is now a list you must access it by index like this: 由于天气现在是一个列表,因此您必须按如下所示通过索引进行访问:
ShowWeather.Text = data.weather[0].description;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.