1

Estou tentando utilizar o DeserializeObject, para gerar objetos através do result de um 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
 }
}

Estou fazendo o seguinte comando para tentar gerar os objetos:

string content = respToken.Content.ReadAsStringAsync().Result;

ObjectWeather weather = JsonConvert.DeserializeObject<ObjectWeather>(content);

através dessa classe:

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; }
}

E estou recebendo o seguinte erro:

{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.

Precisava criar um objeto que possuísse o main, weather e dt_txt, desse retorno Json

4
  • Seu JSON está vindo exatamente como colocou aqui na pergunta? Ele possui erros: Error:Expecting object or array, not string.[Code 1, Structure 1] / Error:Expecting comma or ], not }.[Code 6, Structure 111]. Se quiser fazer teste validando seu JSON usei essa ferramenta: jsonformatter.curiousconcept.com 11/06/2019 às 20:21
  • @GeorgeWurthmann coloquei exatamente como me retorna agora
    – Matheus
    11/06/2019 às 20:28
  • 1
    "main" é um objeto simples e não uma lista 11/06/2019 às 20:32
  • @LeandroAngelo, é que eu diminui a quantidade para a pergunta não ficar muito grande, na verdade ele possui vários main, colocarei na pergunta para ficar mais claro.
    – Matheus
    11/06/2019 às 20:35

1 Resposta 1

1

O Leandro está correto no comentário o main está como objeto simples:

"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
         }

Se main fosse um array estaria no formato abaixo como a própria mensagem de erro te indicou:

"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
         }]

Se main não deve ser um array, mude o trecho abaixo:

public class WeatherList
{
    //public List<Main> main { get; set; } //Mude aqui isso
    public Main main { get; set; } //Para isso
    public List<Weather> weather { get; set; } 
}
1
  • Realmente o Leandro estava certo, era apenas esse detalhe.
    – Matheus
    11/06/2019 às 20:57

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .