C# Json Jira

SSinhg 286 Reputation points
2021-03-24T07:23:56.387+00:00

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

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,994 questions
{count} votes

Accepted answer
  1. craig w 81 Reputation points
    2021-03-24T10:40:16.583+00:00

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

    var jsonContent = JsonConvert.DeserializeObject<List<JiraResponse>>(jsonString);
    

1 additional answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-03-25T09:25:12.7+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.