0

I have json being returned from an API. The JSON is formatted as below:

{
   "success":true,
   "code":200,
   "total":2,
   "data":{
      "1019588":{
         "name":"(t) Bob Jones",
         "calls":213,
         "user_id":"1019588"
      },
      "1019741":{
         "name":"(t) Chris Smith",
         "calls":387,
         "user_id":"1019741"
      }
    }
}

I am trying to deserialize into a C# class but I am having issues with the dynamic id for each employee row.

My code:

AgentPeformanceResponse viewModel = JsonSerializer.Deserialize<AgentPeformanceResponse>(result.Result);


public class AgentPeformanceResponse
    {
        public bool success { get; set; }
        public int code { get; set; }
        public int total { get; set; }
        public Data data { get; set; }

        public AgentPeformanceResponse()
        {
            data = new Data();
        }

    }
    public class Data
    {
        public Data()
        {
            PerformanceReponse = new List<PerformanceReponse>();
        }        
        public List<PerformanceReponse> PerformanceReponse { get; set; }

    }
    public class PerformanceReponse
    {
        public string name { get; set; }
        public int calls { get; set; }
        public string user_id { get; set; }

    }

How do I handle the dynamic employee ID so that I can deserialize it all into one object?

Greg
  • 1
  • 1
  • You would deserialize the `data` elements as a `Dictionary`. See: https://stackoverflow.com/questions/13517792/deserializing-json-with-dynamic-keys and https://stackoverflow.com/questions/24771804/how-can-i-deserialize-a-child-object-with-dynamic-numeric-key-names You can additionally create a custom deserializer if you want those `key`s to be properties in an object, and not a string key of a dictionary – Jonathan Jul 11 '22 at 17:55

3 Answers3

0

all code you really need

Dictionary<string, PerformanceReponse> dict = JsonDocument.Parse(json).RootElement
.GetProperty("data").Deserialize<Dictionary<string,PerformanceReponse>>();

//or if you want a list
List<PerformanceReponse> list = data.Select(d=>d.Value).ToList();

or using Newtonsoft.Json


Dictionary<string,PerformanceReponse> dict =  JObject.Parse(json)
["data"].ToObject<Dictionary<string,PerformanceReponse>>();

how to use

PerformanceReponse data1019588= dict["1019588"];
Serge
  • 40,935
  • 4
  • 18
  • 45
0

You should use a Dictionary:

public class AgentPeformanceResponse
{
    public bool success { get; set; }
    public int code { get; set; }
    public int total { get; set; }
    public Dictionary<string,PerformanceReponse> data { get; set; }
}

public class PerformanceReponse
{
    public string name { get; set; }
    public int calls { get; set; }
    public string user_id { get; set; }
}

Example:

string json = @"{
   ""success"":true,
   ""code"":200,
   ""total"":2,
   ""data"":{
      ""1019588"":{
         ""name"":""(t) Bob Jones"",
         ""calls"":213,
         ""user_id"":""1019588""
      },
      ""1019741"":{
         ""name"":""(t) Chris Smith"",
         ""calls"":387,
         ""user_id"":""1019741""
      }
    }
}";

var obj = System.Text.Json.JsonSerializer
    .Deserialize<AgentPeformanceResponse>(json);
Magnetron
  • 7,495
  • 1
  • 25
  • 41
0
using System;
using System.Collections.Generic;

public class AgentPeformanceResponse
{
    public bool success { get; set; }
    public int code { get; set; }
    public int total { get; set; }
    public Dictionary<string, PerformanceReponse> data { get; set; }
}

public class PerformanceReponse
{
    public string name { get; set; }
    public int calls { get; set; }
    public string user_id { get; set; }
}

public class Program
{
    public static void Main()
    {
        var json = "{\"success\":true,\"code\":200,\"total\":2,\"data\":{\"1019588\":{\"name\":\"(t) Bob Jones\",\"calls\":213,\"user_id\":\"1019588\"},\"1019741\":{\"name\":\"(t) Chris Smith\",\"calls\":387,\"user_id\":\"1019741\"}}}";
        var result = System.Text.Json.JsonSerializer.Deserialize<AgentPeformanceResponse>(json);
        Console.WriteLine(result.code);
        Console.WriteLine(result.data["1019741"].name);
    }
}

The output will be

200

(t) Chris Smith

Fiddle for you https://dotnetfiddle.net/lQu2Ln

Sergey Sosunov
  • 4,124
  • 2
  • 11
  • 15