question

SinghSg avatar image
0 Votes"
SinghSg asked DanielZhang-MSFT answered

C# Naviagate Object inside List

Hi,

This is the JSON response I'm getting from Jira API 3 URL

How do I Navigate and populate schema?

 // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
     public class Schema
     {
         public string type { get; set; }
         public string custom { get; set; }
         public int customId { get; set; }
         public string system { get; set; }
         public string items { get; set; }
     }

 public class Root
 {
     public string id { get; set; }
     public string key { get; set; }
     public string name { get; set; }
     public bool custom { get; set; }
     public bool orderable { get; set; }
     public bool navigable { get; set; }
     public bool searchable { get; set; }
     public List<string> clauseNames { get; set; }
     public string untranslatedName { get; set; }
     public Schema schema { get; set; }
 }


https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-fields/#api-rest-api-3-field-search-get




 [
   {
     "id": "description",
     "name": "Description",
     "custom": false,
     "orderable": true,
     "navigable": true,
     "searchable": true,
     "clauseNames": [
       "description"
     ],
     "schema": {
       "type": "string",
       "system": "description"
     }
   },
   {
     "id": "summary",
     "key": "summary",
     "name": "Summary",
     "custom": false,
     "orderable": true,
     "navigable": true,
     "searchable": true,
     "clauseNames": [
       "summary"
     ],
     "schema": {
       "type": "string",
       "system": "summary"
     }
   }
 ]


dotnet-csharp
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.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi SwatantraSingh-2674,
You can add to a list and then access them.

 List<Schema> schemas = new List<Schema>();
               
 Root[] data = JsonConvert.DeserializeObject<Root[]>(responseString);
  foreach (var x in data)
 {
           roots.Add(x.schema);
 }
       
    
 foreach (var x in schemas)
 {
        Console.WriteLine(x.type+x.system);
    
 }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.

ArtemiyMorozUA avatar image
0 Votes"
ArtemiyMorozUA answered SinghSg commented

hi there! You see that you actually have the array of Roots. So I would suggest trying to do:
Root[] myDeserializedClass = JsonConvert.DeserializeObject<Root[]>(myJsonResponse);

Artem

· 1
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.

Thanks @ArtemiyMorozUA
My code to deserialize
List<EntityJiraField> jsonContent = JsonConvert.DeserializeObject<List<EntityJiraField>>(jiraResponse);

This works fine.
Problem - how do I access the schema
I tried creating an schema object and then adding but it didn't work
Error "Object Reference not set to instance ..."

something like this

 Schema schema = new Schema();

var test = jsonContent[item].schema.@type; //eRROR line





0 Votes 0 ·