C# JSON Desearlization dynamic reponse

SSinhg 286 Reputation points
2022-09-09T10:49:07.993+00:00

Hi All,

Can I loop through all the fields without mentioning their names.. don't want to hardcode the field names... Is it possible?
i.e. read response in a dynamic object and the loop through - not sure how hypothetical I am being :)

public class Fields
{
public Parent parent { get; set; }
public Issuetype issuetype { get; set; }
public Customfield10091 customfield_10091 { get; set; }
public Customfield10092 customfield_10092 { get; set; }
}

I just want to loop through the fields and check / populate their names in List to export on excel

Used this to generate C# classes response object

My C# code to desearlize the response...

 using (StreamReader r = new StreamReader("SandBox_metaData.json"))  
            {  
                string json = r.ReadToEnd();  
                //Reads through the response to create populate the relevant classes   
                var items = JsonConvert.DeserializeObject<List<Root>>(json);  
  

Part B in the code where I have tried various ticks to loop but nothing works...

for (int i = 0; i < items[0].projects[0].issuetypes.Count; i++)  
                {                      
                    PropertyInfo[] properties = typeof(Fields).GetProperties(); // If I declare a class with all the props then this works fine... but what if a new field is add then this won't work.  
                    int j = 1;  
                    foreach (PropertyInfo property in properties)  
                    {  
                        Console.WriteLine("{0} {1}", property.Name, items[0].projects[0].issuetypes[j].fields.**customfield_10213**); //I want to keep the feilds list dynamic populate.  
                        var items1 = items[0].projects[0].issuetypes[j].fields;                          
                        foreach (var item in items)  
                        {  
                            var tesdfsdf = item.ToString();  
  
                        }  
                        j++;  
                    }  
                }  

Below is the JSON response from Jira API here

{  
  "expand": "projects",  
  "projects": [  
    {  
      "expand": "issuetypes",  
      "self": "https://someJiraProject.atlassian.net/rest/api/3/project/10014",  
      "id": "10014",  
      "key": "Rocket",  
      "name": "Rocket Program",        
      "issuetypes": [  
        {  
          "self": "https://someJiraProject.atlassian.net/rest/api/3/issuetype/11494",  
          "id": "11494",  
          "description": "A collection of related bugs, stories, and tasks.",  
          "iconUrl": "https://someJiraProject.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10304?size=medium",  
          "name": "NASA",  
          "untranslatedName": "NASA",  
          "subtask": false,  
          "expand": "fields",  
          "fields": {  
            "parent": {  
              "required": false,  
              "schema": {  
                "type": "issuelink",  
                "system": "parent"  
              },  
              "name": "Parent",  
              "key": "parent",  
              "hasDefaultValue": false,  
              "operations": [  
                "set"  
              ]  
            },  
            "issuetype": {  
              "required": true,  
              "schema": {  
                "type": "issuetype",  
                "system": "issuetype"  
              },  
              "name": "Issue Type",  
              "key": "issuetype",  
              "hasDefaultValue": false,  
              "operations": [],  
              "allowedValues": [  
                {  
                  "self": "https://someJiraProject.atlassian.net/rest/api/3/issuetype/11494",  
                  "id": "11494",  
                  "description": "A collection of related bugs, stories, and tasks.",  
                  "iconUrl": "https://someJiraProject.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10304?size=medium",  
                  "name": "NC",  
                  "subtask": false,  
                  "avatarId": 10304,  
                  "hierarchyLevel": 1  
                }  
              ]  
            },  
            "customfield_10091": {  
              "required": false,  
              "schema": {  
                "type": "date",  
                "custom": "com.atlassian.jira.plugin.system.customfieldtypes:datepicker",  
                "customId": 10091  
              },  
              "name": "Rockets ODD",  
              "key": "customfield_10091",  
              "hasDefaultValue": false,  
              "operations": [  
                "set"  
              ]  
            },  
            "customfield_10092": {  
              "required": false,  
              "schema": {  
                "type": "string",  
                "custom": "com.atlassian.jira.plugin.system.customfieldtypes:textarea",  
                "customId": 10092  
              },  
              "name": "Rockets Comment",  
              "key": "customfield_10092",  
              "hasDefaultValue": false,  
              "operations": [  
                "set"  
              ]  
            }  
          }  
        }  
      ]  
    }  
  ]  
}  


// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);  
    public class AllowedValue  
    {  
        public string self { get; set; }  
        public string id { get; set; }  
        public string description { get; set; }  
        public string iconUrl { get; set; }  
        public string name { get; set; }  
        public bool subtask { get; set; }  
        public int avatarId { get; set; }  
        public int hierarchyLevel { get; set; }  
    }  
  
    public class AvatarUrls  
    {  
        public string _48x48 { get; set; }  
        public string _24x24 { get; set; }  
        public string _16x16 { get; set; }  
        public string _32x32 { get; set; }  
    }  
  
    public class Customfield10091  
    {  
        public bool required { get; set; }  
        public Schema schema { get; set; }  
        public string name { get; set; }  
        public string key { get; set; }  
        public bool hasDefaultValue { get; set; }  
        public List<string> operations { get; set; }  
    }  
  
    public class Customfield10092  
    {  
        public bool required { get; set; }  
        public Schema schema { get; set; }  
        public string name { get; set; }  
        public string key { get; set; }  
        public bool hasDefaultValue { get; set; }  
        public List<string> operations { get; set; }  
    }  
  
    public class Fields  
    {  
        public Parent parent { get; set; }  
        public Issuetype issuetype { get; set; }  
        public Customfield10091 customfield_10091 { get; set; }  
        public Customfield10092 customfield_10092 { get; set; }  
    }  
  
    public class Issuetype  
    {  
        public string self { get; set; }  
        public string id { get; set; }  
        public string description { get; set; }  
        public string iconUrl { get; set; }  
        public string name { get; set; }  
        public string untranslatedName { get; set; }  
        public bool subtask { get; set; }  
        public string expand { get; set; }  
        public Fields fields { get; set; }  
    }  
  
    public class Issuetype2  
    {  
        public bool required { get; set; }  
        public Schema schema { get; set; }  
        public string name { get; set; }  
        public string key { get; set; }  
        public bool hasDefaultValue { get; set; }  
        public List<object> operations { get; set; }  
        public List<AllowedValue> allowedValues { get; set; }  
    }  
  
    public class Parent  
    {  
        public bool required { get; set; }  
        public Schema schema { get; set; }  
        public string name { get; set; }  
        public string key { get; set; }  
        public bool hasDefaultValue { get; set; }  
        public List<string> operations { get; set; }  
    }  
  
    public class Project  
    {  
        public string expand { get; set; }  
        public string self { get; set; }  
        public string id { get; set; }  
        public string key { get; set; }  
        public string name { get; set; }  
        public AvatarUrls avatarUrls { get; set; }  
        public List<Issuetype> issuetypes { get; set; }  
    }  
  
    public class Root  
    {  
        public string expand { get; set; }  
        public List<Project> projects { get; set; }  
    }  
  
    public class Schema  
    {  
        public string type { get; set; }  
        public string system { get; set; }  
        public string custom { get; set; }  
        public int customId { get; set; }  
    }  
  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2022-09-09T16:08:00.08+00:00

    You defined the json class structure wrong. “fields” is a hash map and should be defined as

    public class Issuetype  
         {  
             public string self { get; set; }  
             public string id { get; set; }  
             public string description { get; set; }  
             public string iconUrl { get; set; }  
             public string name { get; set; }  
             public string untranslatedName { get; set; }  
             public bool subtask { get; set; }  
             public string expand { get; set; }  
             public Dictionary<string, Field> fields { get; set; }  
         }  
    

    The class Field should have all the field properties

       public class Field  
         {  
             public bool required { get; set; }  
             public Schema schema { get; set; }  
             public string name { get; set; }  
             public string key { get; set; }  
             public bool hasDefaultValue { get; set; }  
             public List<object> operations { get; set; }  
             public List<AllowedValue> allowedValues { get; set; }  
         }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful