简体   繁体   English

无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”})

[英]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.

相关问题 无法将JSON数组(例如[1,2,3])反序列化为类型&#39;&#39;,因为类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;Value[]&#39;,因为该类型需要 JSON 数组(例如 [1,2,3]) - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Value[]' because the type requires a JSON array (e.g. [1,2,3]) 无法反序列化当前 JSON object 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“ ”,因为类型需要 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})才能正确反序列化 - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“”,因为类型需要 JSON object(例如,将 {“name”} 正确地反序列化为“value”: - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON ZA8CFDE6331BD59EB2AC96F8911ZC4 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleAppTest01.Location' because the type requires a JSON object 如何修复“无法将当前的 JSON 数组反序列化为“段落”类型,因为需要 JSON object(例如 {“name”:“值”) - How to fix 'Cannot deserialize the current JSON array into type 'passages' because requires a JSON object (e.g. {“name”:“value”}) to deserialize 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 无法将当前JSON对象反序列化为类型&#39;WindowsService1 ...需要JSON数组(例如[1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object into type 'WindowsService1…requires a JSON array (e.g. [1,2,3]) to deserialize correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM