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

247
Visualizações
How to deserialize JSON with properties named "1" and "2"? These aren't valid property names in C#

I am calling a REST API from C# where the class name is a number, which isn't allowed in C#. I'm not sure how I can get around this? I'm using Newtonsoft for the serialization.

This is the JSON from my REST API:

{
  "customfield_10323": {
    "1": [
      "com.atlassian.greenhopper.service.sprint.Sprint@2442b4ee[id=78,rapidViewId=21,state=CLOSED,name=APPLE A5 (27/07 - 07/08),startDate=2020-07-27T08:00:00.000+01:00,endDate=2020-08-07T17:00:00.000+01:00,completeDate=2020-08-13T11:22:53.314+01:00,sequence=78,goal=]",
      "com.atlassian.greenhopper.service.sprint.Sprint@54cfb92f[id=107,rapidViewId=19,state=ACTIVE,name=ORANGE B2 (08/03 - 19/03),startDate=2021-03-08T08:00:00.000Z,endDate=2021-03-19T17:00:00.000Z,completeDate=<null>,sequence=107,goal=]"
    ],
    "2": [
      {
        "id": 78,
        "name": "APPLE A5 (27/07 - 07/08)",
        "state": "closed",
        "boardId": 21
      },
      {
        "id": 107,
        "name": "ORANGE B2 (08/03 - 19/03)",
        "state": "active",
        "boardId": 19
      }
    ]
  }
}

Which I've translated to this:

public class 2
{
    public int id { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public int boardId { get; set; }
}

public class Customfield10323
{
    public List<string> _1 { get; set; }
    public List<2> 2 { get; set; }
}

public class Root
{
    public Customfield10323 customfield_10323 { get; set; }
}

But I'm not sure this is correct because I can't call the class 2 because it violates naming conventions. Any help would be appreciated as I'm really stuck!

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

0

Modify your classes to use the JsonProperty attribute:

public class Two // valid class name
{
    public int id { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public int boardId { get; set; }
}

public class Customfield10323
{
    [JsonProperty("1")] // property name in JSON
    public List<string> One { get; set; } // valid property name

    [JsonProperty("2")] // property name in JSON
    public List<Two> Two { get; set; } // valid property name
}

public class Root
{
    public Customfield10323 customfield_10323 { get; set; }
}
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