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!
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; }
}