question

SinghSg avatar image
0 Votes"
SinghSg asked craigw-8923 commented

C# Json Jira

Hi All,

I'm using Jira API to pull date from Jira and populate on Excel

Problem - I'm not to deserialize the response

I used this to create the response object
https://json2csharp.com/

Verified Json using this http://json.parser.online.fr/


can comeone please help me with a reference link or sample code

  private static async Task SearchJira()
         {
             try
             {
                 string apiUrl = @"https://URL/rest/api/3/search?jql=project=ProjectName&maxResults=10";
    
    
                 using (var httpClient = new HttpClient())
                 {
                     using (var request = new HttpRequestMessage(new HttpMethod("GET"), apiUrl))
                     {
                         var base64authorization =
                             Convert.ToBase64String(Encoding.ASCII.GetBytes("emailId:CU2NhBYhT2Di48DA9"));
                         request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
    
                         var response = await httpClient.SendAsync(request);
    
 // I can Json response in this variable 
                         var jsonString = await response.Content.ReadAsStringAsync();
                          
 // How to populate the response object
 var jsonContent = JsonConvert.DeserializeObject<JiraResponse>(jsonString);
                     }
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }


Now I need to populate the list so that I can intern populate my excel and move forward with code...


Appreciate the help

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


In Visual Studio, when you debug the program, copy the value of jsonString to Clipboard, go to some new class file, select from menu: Edit, Paste Special, Paste JSON as Classes, and then use these new classes and System.Text.JsonSerializer (added by "Manage NuGet Packages" from Project menu) to deserialise, then usually it works.

Maybe you should show an example of your JSON string. What errors or problem did you observe with your current code?

0 Votes 0 ·
craigw-8923 avatar image
0 Votes"
craigw-8923 answered craigw-8923 commented

It sounds like you're dealing with a collection so try:

 var jsonContent = JsonConvert.DeserializeObject<List<JiraResponse>>(jsonString);
· 2
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 anonymous user
I had tried it ...
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[JiraWinFormApp.JiraResponse]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.


Json response from jira....


81401-capture.png


0 Votes 0 ·
capture.png (33.2 KiB)

Ok, so first you need to define the top level object to deserialize the response into,

e.g.

 class JiraResponse {
   string  Expand {get;set;}
   int StartAt {get;set;}
   int MaxResults {get;set;}
   int Total {get;set;}
   List<JiraIssue> Issues {get;set;}
 }

Then the JiraIssue class needs to be shaped like each of the JSON obejcts in the issues collection.

1 Vote 1 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi SwatantraSingh-2674,
Please try to use string instead of var, as now it inferred the type as Task<string>.
Change var jsonString = await response.Content.ReadAsStringAsync();
To

 String jsonString = await response.Content.ReadAsStringAsync();

Or

 String jsonString = response.Content.ReadAsStringAsync().Result;

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.