Stan Venne

Stan Venne

  • NA
  • 8
  • 587

json deserialization

Mar 31 2021 10:25 PM
I have some json returned form and API in the following format and I'm trying to pull the separate "items" into a list of items:
 
{"meta":{"page":1,"count":41,"pageCount":1,"totalCount":41},
"items":[
{
"agentId":"007",
"presence":{"since":"2020-06-25T18:42:41.871Z","name":"Ready","description":"Ready","eligibleForRouting":true},
"workload":{"since":"2020-06-29T13:08:38.666Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1000",
"presence":{"since":"2021-03-08T18:15:37.567Z","name":"ExtendedAway","description":"In Meeting","eligibleForRouting":false},
"workload":{"since":"2020-12-31T19:52:06.495Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1001",
"presence":{"since":"2021-03-29T15:39:19.401Z","name":"ExtendedAway","description":"In Meeting","eligibleForRouting":false},
"workload":{"since":"2021-03-04T19:46:10.092Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1002",
"presence":{"since":"2021-03-31T15:09:13.699Z","name":"Away","description":"Break","eligibleForRouting":false},
"workload":{"since":"2021-03-31T15:06:09.804Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1003",
"presence":{"since":"2021-03-31T14:28:52.187Z","name":"Ready","description":"Ready","eligibleForRouting":true},
"workload":{"since":"2021-03-31T15:19:25.908Z","calls":1,"semiLive":0,"nonLive":0},
"interaction":[{
"guid":"017888bc-5c0b-410a-a389-fac8f9423457",
"channelGuid":null,
"since":"2021-03-31T15:19:23.763Z",
"medium":"Phone",
"mediumManager":"VCC",
"direction":"Inbound",
"state":"Wrap",
"reason":null}]},
{
"agentId":"1004",
"presence":{"since":"2021-01-15T20:43:44.391Z","name":"ExtendedAway","description":"In Meeting","eligibleForRouting":false},
"workload":{"since":"2020-07-01T20:19:36.049Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1005",
"presence":{"since":"2021-03-31T15:03:30.458Z","name":"ExtendedAway","description":"Projects","eligibleForRouting":false},
"workload":{"since":"2021-03-31T13:33:22.917Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1006",
"presence":{"since":"2021-03-30T21:30:34.339Z","name":"LoggedOut","description":"Logged Out","eligibleForRouting":false},
"workload":{"since":"2021-03-30T20:30:17.244Z","calls":0,"semiLive":0,"nonLive":0},
"interaction":[]},
{
"agentId":"1007",
"presence":{"since":"2021-03-31T14:19:25.260Z","name":"Ready","description":"Ready","eligibleForRouting":true},
"workload":{"since":"2021-03-31T15:10:10.770Z","calls":1,"semiLive":0,"nonLive":0},
"interaction":[{
"guid":"017888b9-3108-467d-8c2f-a82c94100183",
"channelGuid":null,
"since":"2021-03-31T15:10:13.779Z",
"medium":"Phone",
"mediumManager":"VCC",
"direction":"Inbound",
"state":"Wrap",
"reason":null}]},
{
"agentId":"1008",
"presence":{"since":"2021-03-31T14:29:19.543Z","name":"Ready","description":"Ready","eligibleForRouting":true},
"workload":{"since":"2021-03-31T15:14:46.599Z","calls":1,"semiLive":0,"nonLive":0},
"interaction":[{
"guid":"017888bf-c715-4c18-b2af-e3bc52e90561",
"channelGuid":null,
"since":"2021-03-31T15:14:54.237Z",
"medium":"Phone",
"mediumManager":"VCC",
"direction":"Inbound",
"state":"Connected",
"reason":null}]}
}
 
I have custom classes as follows:
  1. public class AgentStatus  
  2. {  
  3. [JsonProperty("agentId")]  
  4. public string AgentID { getset; }  
  5. [JsonProperty("presence")]  
  6. public Presence Presence { getset; }  
  7. [JsonProperty("workload")]  
  8. public Workload Workload { getset; }  
  9. [JsonProperty("interaction")]  
  10. public Interaction Interaction { getset; }  
  11. }  
  12. public class Presence  
  13. {  
  14. [JsonProperty("since")]  
  15. public string Since { getset; }  
  16. [JsonProperty("name")]  
  17. public string Name { getset; }  
  18. [JsonProperty("description")]  
  19. public string Description { getset; }  
  20. [JsonProperty("eligibleForRouting")]  
  21. public bool EligibleForRouting { getset; }  
  22. }  
  23. public class Workload  
  24. {  
  25. [JsonProperty("since")]  
  26. public string Since { getset; }  
  27. [JsonProperty("calls")]  
  28. public string Calls { getset; }  
  29. [JsonProperty("semiLive")]  
  30. public bool SemiLive { getset; }  
  31. [JsonProperty("nonLive")]  
  32. public bool NonLive { getset; }  
  33. }  
  34. public class Interaction  
  35. {  
  36. [JsonProperty("guid")]  
  37. public string Guid { getset; }  
  38. [JsonProperty("channelGuid")]  
  39. public string ChannelGuid { getset; }  
  40. [JsonProperty("since")]  
  41. public string Since { getset; }  
  42. [JsonProperty("medium")]  
  43. public string Medium { getset; }  
  44. [JsonProperty("mediumManager")]  
  45. public string MediumManager { getset; }  
  46. [JsonProperty("direction")]  
  47. public string Direction { getset; }  
  48. [JsonProperty("state")]  
  49. public string State { getset; }  
  50. [JsonProperty("reason")]  
  51. public string Reason { getset; }  
  52. }  
I'm trying to deserialize with the following code:
  1. IRestResponse response = client.Execute(request);  
  2. dynamic json = JsonConvert.DeserializeObject<List<AgentStatus>>(response.Content.ToString());  
But get an error: "Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`"
 
I'm admittedly very new to working with JSON responses.

Answers (3)