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

240
Vistas
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 Respuestas
Responde la pregunta

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