1

I am getting below json for api.

{
  "performances": [
    {
      "Employee1": {
        "firstname": "xxx",
        "value": 2678       
      },
      "Employee2": {
        "firstname": "yyy",
        "value": 2676
       
      },
      "starttime": "2021-04-09T14:48:00Z",
      "endtime": "2022-04-09T14:49:00Z"
    },
    {
      "Employee1": {
        "firstname": "xxx",
        "value": 3354 
      },
      "Employee2": {
        "firstname": "yyy",
        "value": 234
      },
      "Employee3": {
        "firstname": "qqq",
        "value": 2344
      },
      "starttime": "2022-04-09T14:49:00Z",
      "endtime": "2023-04-09T14:50:00Z"
    }
  ]
}

tried Creating Dictionary<string, employee>. but starttime and endtime gives the problem.

Can someone help me to Deserializing in .Net core using c#

i tried creating below classes. nothing worked. I was getting null for all values

public class Variable
    {
        public string firstname{ get; set; }
        public double value{ get; set; }
    }
    public class Custom
    {
        public List<Dictionary<string, Variable>> response { get; set; }
        public string starttime { get; set; }
        public string endtime { get; set; }
    }
    public class CustomerList
    {       
        public List<Custom> performances { get; set; }
    }
Vels
  • 23
  • 4
  • Use `[JsonExtensionData]` attribute – Charlieface Apr 17 '23 at 13:36
  • Since the values of your unknown-in-advance `Employee*` properties are typed as `Variable` rather than free-form JSON, possibly [this answer](https://stackoverflow.com/a/40094403/3744182) to [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/q/40088941/3744182) does what you need. – dbc Apr 17 '23 at 19:34

1 Answers1

0

One option is to use JsonExtensionData and process it "dynamically":

Newtonsoft Json.NET:

foreach (var custom in customerList.performances.ToList())
{
    foreach (var kvp in custom.ExtensionData)
    {
        Console.WriteLine(kvp.Key);
        Console.WriteLine((kvp.Value as Newtonsoft.Json.Linq.JObject).ToObject<Variable>().firstname);
    }
}

// the rest classes are the same
public class Custom
{
    [JsonExtensionData]
    public Dictionary<string, JToken>? ExtensionData { get; set; }
    public string starttime { get; set; }
    public string endtime { get; set; }
}

System.Text.Json:

var customerList = JsonSerializer.Deserialize<CustomerList>(js1);

foreach (var custom in customerList.performances.ToList())
{
    foreach (var kvp in custom.ExtensionData)
    {
        Console.WriteLine(kvp.Key);
        Console.WriteLine(kvp.Value.Deserialize<Variable>().firstname);
    }
}

public class Custom
{
    [JsonExtensionData]
    public Dictionary<string, JsonElement>? ExtensionData { get; set; }
    public string starttime { get; set; }
    public string endtime { get; set; }
}

Otherwise you will need to write a custom converter (System.Text.Json).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132