question

SinghSg avatar image
0 Votes"
SinghSg asked Viorel-1 commented

C# JSON Desearlization dynamic reponse

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







dotnet-csharpdotnet-runtime
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SinghSg, Welcome to Microsoft Q&A, based on my test, the answer from the Bruce is correct. The following is some additions for the answer.
First, Please add the two classes in the answer and remove the class Parent, Fields, Customfield10092,Customfield10091.
Second, you could use the following code to get the field in a loop:

 var items = JsonConvert.DeserializeObject<Root>(json);
             foreach (var item in items.projects[0].issuetypes)
             {
                 foreach (var fie in item.fields)
                 {
                     Console.WriteLine(fie.Key);
                 }
             }

output:

239867-image.png


1 Vote 1 ·
image.png (2.3 KiB)
SinghSg avatar image SinghSg JackJJun-MSFT ·

Thanks @JackJJun-MSFT

Appreciate any help on this.

Agree with you, I worked on @Bruce-SqlWork response and managed to reach to your code. For getting Key & Name. For e.g.
"name": "Rockets ODD"
"key": "customfield_10091"


 Console.WriteLine(fie.Key, fie.Value.name);






0 Votes 0 ·

239517-issuebinding.png

I am not able to loop through the response as its not collection. how do i do it?

0 Votes 0 ·
issuebinding.png (110.0 KiB)

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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; }
      }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.