1
I’m trying to use the DeserializeObject
, to generate objects through the result
json:
{
"cod": "200",
"message": 0.0068,
"cnt": 40,
"list": [
{
"dt": 1560286800,
"main": {
"temp": 17.14,
"temp_min": 17.14,
"temp_max": 17.21,
"pressure": 1019.2,
"sea_level": 1019.2,
"grnd_level": 994.29,
"humidity": 93,
"temp_kf": -0.07
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "céu limpo",
"icon": "01n"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 0.99,
"deg": 64.629
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-11 21:00:00"
},
{
"dt": 1560297600,
"main": {
"temp": 15.7,
"temp_min": 15.7,
"temp_max": 15.75,
"pressure": 1021.33,
"sea_level": 1021.33,
"grnd_level": 996,
"humidity": 98,
"temp_kf": -0.05
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "céu limpo",
"icon": "01n"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 0.59,
"deg": 305.447
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-12 00:00:00"
},
{
"dt": 1560308400,
"main": {
"temp": 15.11,
"temp_min": 15.11,
"temp_max": 15.15,
"pressure": 1021.58,
"sea_level": 1021.58,
"grnd_level": 996.15,
"humidity": 97,
"temp_kf": -0.04
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "céu pouco nublado",
"icon": "02n"
}
],
"clouds": {
"all": 12
},
"wind": {
"speed": 0.5,
"deg": 257.297
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-12 03:00:00"
}
],
"city": {
"id": 3459712,
"name": "Joinville",
"coord": {
"lat": -26.3045,
"lon": -48.8487
},
"country": "BR",
"population": 461304,
"timezone": -10800
}
}
I am making the following command to try to generate the objects:
string content = respToken.Content.ReadAsStringAsync().Result;
ObjectWeather weather = JsonConvert.DeserializeObject<ObjectWeather>(content);
through that class:
public class ObjectWeather
{
public List<WeatherList> list { get; set; }
}
public class WeatherList
{
public List<Main> main { get; set; }
public List<Weather> weather { get; set; }
}
public class Main
{
public decimal temp { get; set; }
public decimal temp_min { get; set; }
public decimal temp_max { get; set; }
}
public class Weather
{
public string description { get; set; }
}
And I’m getting the following mistake:
{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[HBSIS.Models.WeatherViewModels.Main]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
He needed to create an object that possessed the main
, weather
and dt_txt
, of that return Json
Is your JSON coming exactly as you put it here in the question? It has errors:
Error:Expecting object or array, not string.[Code 1, Structure 1]
/Error:Expecting comma or ], not }.[Code 6, Structure 111]
. If you want to test validating your JSON I used this tool: https://jsonformatter.curiousconcept.com/– George Wurthmann
@Georgewurthmann put exactly as I return now
– Matheus
"main" is a simple object and not a list
– Leandro Angelo
@Leandroangelo, is that I decrease the amount so the question does not get too big, in fact it possesses several
main
, I’ll ask the question to be clearer.– Matheus