Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
Using HttpContent.ReadAsAsync<T> to parse responses with one object or an array

I'm integrating to an API that returns this model from almost endpoints

{
  meta: { ... },
  data: { ... }
}

But for some calls, the data is an array of the same kind of objects

{
  meta: { ... },
  data: [
    { ... },
    { ... }
  ]
}

I want to use HttpContent.ReadAsAsync<ResponseObj> to convert both of these to my C# classes, and the I've set up the Response class like this:

public class ResponseObj {
  public MetaObj Meta {get;set;}
  public DataObj[] Data {get;set;}
}

Somewhat expectedly, I get an exception when trying to parse the first response. Is it possible to tell the JSON parser to handle the single data object and return a single-element array?

The only other solution I can see is to create separate ResponseObj definitions for the two different response types.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Create your ResponseObj as a generic class.

public class ResponseObj<T> {
  public MetaObj Meta {get;set;}
  public T Data {get;set;}
}

You can deserialize json using HttpContent.ReadAsAsync<ResponseObj<DataObj>> or HttpContent.ReadAsAsync<ResponseObj<DataObj[]>>

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda