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
As you can see in the response, "requesters" element is actually array. So according response you should have c# objects like this:
and then deserialize you response like:
If you want to have nice c# variables names, use Attributes like this:
Btw, you can simply find online convertors from json to c# classes.
you have to fix the type of these properties
and use this code to deserialize
you can not get a requester because your requester is a list of PersonInfoJson objects