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

229
Visualizações
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 Respostas
Responde à pergunta

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