skip to Main Content

When i use the following:

using (HttpResponseMessage response = client.GetAsync(postMethod.ToString()).Result)
{
    if (response.IsSuccessStatusCode)
    {
        var Json = response.Content.ReadAsStringAsync().Result;
        Console.WriteLine(Json);

        var y = JsonSerializer.Deserialize<PersonInfoJson>(Json);
        Console.Write("r= " + y.requester); 
        Console.Write("id= " + y.id);
    }
}

The json returned is

{"requesters":[{"active":true,"address":null,"background_information":null,"can_see_all_changes_from_associated_departments":false,"can_see_all_tickets_from_associated_departments":false,"created_at":"2022-12-15T21:07:22Z","custom_fields":{"blah_id":"00002342"},"department_ids":[1111111],"department_names":["Student"],"external_id":null,"first_name":"mike","has_logged_in":false,"id":123456,"is_agent":false,"job_title":null,"language":"en","last_name":"ishere","location_id":null,"location_name":null,"mobile_phone_number":"111-111-1111","primary_email":"[email protected]","reporting_manager_id":null,"secondary_emails":["[email protected]"],"time_format":"12h","time_zone":"Eastern Time (US & Canada)","updated_at":"2023-02-03T16:59:41Z","vip_user":false,"work_phone_number":"222-222-2222"}]}

I am trying to add it to the object above to get the id however that entire json is being dumped into the y.requester variable instead of each of the listed variables like in the following object:

public partial class PersonInfoJson
{
    public string requester { get; set; }
    public string department_names { get; set; }
    public string first_name { get; set; }
    public string job_title { get; set; }

    public string last_name { get; set; }
    public string mobile_phone_number { get; set; }
    public string primary_email { get; set; }
    public string secondary_emails { get; set; }
    public string work_phone_number { get; set; }
    public DateTime updated_at { get; set; }
    public int id { get; set; }
}

I need to get the info after the line section that says {"requesters":

currently var y = JsonSerializer.Deserialize<PersonInfoJson>(Json); puts that entire string into public string requester { get; set; }

2

Answers


  1. As you can see in the response, "requesters" element is actually array. So according response you should have c# objects like this:

    public class CustomFields
    {
        public string blah_id { get; set; }
    }
        
    public class Requester
    {
        public bool active { get; set; }
        public object address { get; set; }
        public object background_information { get; set; }
        public bool can_see_all_changes_from_associated_departments { get; set; }
        public bool can_see_all_tickets_from_associated_departments { get; set; }
        public DateTime created_at { get; set; }
        public CustomFields custom_fields { get; set; }
        public List<int> department_ids { get; set; }
        public List<string> department_names { get; set; }
        public object external_id { get; set; }
        public string first_name { get; set; }
        public bool has_logged_in { get; set; }
        public int id { get; set; }
        public bool is_agent { get; set; }
        public object job_title { get; set; }
        public string language { get; set; }
        public string last_name { get; set; }
        public object location_id { get; set; }
        public object location_name { get; set; }
        public string mobile_phone_number { get; set; }
        public string primary_email { get; set; }
        public object reporting_manager_id { get; set; }
        public List<string> secondary_emails { get; set; }
        public string time_format { get; set; }
        public string time_zone { get; set; }
        public DateTime updated_at { get; set; }
        public bool vip_user { get; set; }
        public string work_phone_number { get; set; }
    }
        
    public class Root
    {
       public List<Requester> requesters { get; set; }
    }
    

    and then deserialize you response like:

    var y = JsonConvert.DeserializeObject<Root>(Json);
    

    If you want to have nice c# variables names, use Attributes like this:

    [JsonProperty(PropertyName = "work_phone_number")]
    public string WorkPhoneNumber { get; set; }
    

    Btw, you can simply find online convertors from json to c# classes.

    Login or Signup to reply.
  2. you have to fix the type of these properties

    
    public partial class PersonInfoJson
    {
        // ... your other properties
    
        public string[] department_names { get; set; }
        public string[] secondary_emails { get; set; }
        
    }
    

    and use this code to deserialize

    var json = response.Content.ReadAsStringAsync().Result;
     
    PersonInfoJson personInfo= JObject.Parse(json)["requesters"]
                             .ToObject<List<PersonInfoJson>>()
                             .FirstOrDefault();
    
     Console.WriteLine("id= " + personInfo.id);
    

    you can not get a requester because your requester is a list of PersonInfoJson objects

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search