Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
System.Text.Json can't deserialize an object with an array of objects?

I have a json structure that looks like this:

{
"SystemType": "SingleDevice",
"Devices":
[
    {
    "DeviceSettings":{
        "DeviceType": "Blah",
        "Generates": 4,
        "RAdd": 10,
        "TAdd": 10,
        "Distance": 1,
        "IpAddress": "192.168.0.21",
        "Port": 50002 
        }
    }
]}

I then have two classes that looks like this:

DeviceSettings.cs

public class DeviceSettings
{
    public string DeviceType { get; set; }
    public int Generates { get; set; }
    public double RAdd{ get; set; }
    public double TAdd { get; set; }
    public double Distance { get; set; }
    public string IpAddress { get; set; }
    public int Port { get; set; }
}

SystemSettings.cs

  public class SystemSettings
{
    public string SystemType { get; set; }
    public DeviceSettings[] Devices { get; set; }
}

When I debug the following code and look at the locals, the resulting object that gets returned exists but with some issues. The SystemType properly displays "SingleDevice", and Devices indicates it's an array of size=1, and provides element 0, but when I look the actual values for everything, they are either null or 0. Specifically, DeviceType and IpAddress = null, everything else is 0. Can anyone explain why and how to get around this?

        static void Main(string[] args)
    {
        var configFilePath = @"C:\Users\Public\Documents\myFolder\SystemConfiguration.json";
        var config = System.IO.File.ReadAllText(configFilePath);
        Console.WriteLine(config);
        var systemSettings = JsonSerializer.Deserialize<SystemSettings>(config);
        Console.ReadKey();
    }

locals output

I've seen posts talking about inferred types, but I believe everything is explicit here. I'm not quite sure what's going on.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are missing an object to hold the DeviceSettings property:

public class SystemSettings
{
    public string SystemType { get; set; }
    public Device[] Devices { get; set; } // <-- change this
}

// Add this
public class Device
{
    public DeviceSettings DeviceSettings { get; set; }

}

public class DeviceSettings
{
    //snip
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda