Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
¿System.Text.Json no puede deserializar un objeto con una matriz de objetos?

Tengo una estructura json que se ve así:

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

Luego tengo dos clases que se ven así:

Configuración del dispositivo.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; } }

Configuración del sistema.cs

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

Cuando depuro el siguiente código y observo los locales, el objeto resultante que se devuelve existe pero con algunos problemas. SystemType muestra correctamente "SingleDevice", y Devices indica que es una matriz de tamaño = 1 y proporciona el elemento 0, pero cuando miro los valores reales para todo, son nulos o 0. Específicamente, DeviceType e IpAddress = null, todo else es 0. ¿Alguien puede explicar por qué y cómo evitar esto?

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

salida de locales

He visto publicaciones que hablan de tipos inferidos, pero creo que todo es explícito aquí. No estoy muy seguro de lo que está pasando.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Te falta un objeto para contener la propiedad DeviceSettings :

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!