-2

This is the sample JSON which I receive from a service, and I am unable to deserialize it using C#.

{
    "columns": [
        "empid",
        "employeename",
        "currentAllocation",
        "Dept head",
        "Manager",
        "department",
        "duration_s",
        "count"
    ],
    "data": [
        [
            1,
            "Jane Doe",
            "Production",
            11,
            "John Doe",
            "Quality",
            26638.0,
            {
                "columns": [
                    "start_timestamp",
                    "duration_s"
                ],
                "data": [
                    [
                        1588351656.54799,
                        450.0
                    ],
                    [
                        1588421798.54799,
                        1438.0
                    ],
                    [
                        1589875223.54799,
                        597.0
                    ]
                ]
            }
        ],
        [
            1,
            "Jane Doe",
            "Production",
            45,
            "Peter Pan",
            "Logistics",
            33379.0,
            {
                "columns": [
                    "start_timestamp",
                    "duration_s"
                ],
                "data": [
                    [
                        1588351656.54799,
                        450.0
                    ],
                    [
                        1588421798.54799,
                        1438.0
                    ],
                    [
                        1589875223.54799,
                        597.0
                    ]
                ]
            }
        ]
    ]
}

I am having a problem defining the target object. Particularly, I am unable to decide the structure/format of the data node. It looks like it should be a List<List<Tuple<string,string,string,string,string,string,Encounter>>> where Encounter is class with two properties:

 public class Encounter
 { 
     public List<string> column 
     public List<Tuple<string,string>> data 
 } 

Can someone kindly suggest a solution?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Anupam Dutta
  • 139
  • 1
  • 7
  • 1
    Are you deserializing to object? You most likely don't have proper target object. Kindly provide more info about the target object, how you try to deserialize it. Nobody can help with the minimal info you provided :) – worldwildwebdev Jul 27 '20 at 09:14
  • You can use the [link](https://www.unserialize.com/s/2f74642f-38d7-4369-4bbc-00005b4d7d08) in order to understand the object for matching to your target object – Nazim Jul 27 '20 at 09:17
  • The classes you need to use for this is not gonna be particularly nice because of the eager use of lists instead of objects, in that json. You're going to have things like `List>` and `List>`, which you're still going to have to pick apart manually. – Lasse V. Karlsen Jul 27 '20 at 09:20
  • However, you need to show the code you tried to use. If you're asking how to actually deserialize this, the answer is to construct classes that correspond to the structure of that JSON and use `JsonConvert.DeserializeObject(json)`, but if you're having problems declaring the class, you need to show what you have so that we can pinpoint your problems. Also, describe what your current problem *is*. – Lasse V. Karlsen Jul 27 '20 at 09:21
  • thanks , I am having problem deciding the target object . I am unable to decide the structure/format of data node . List>> Where encounter is class with two properties public class Encounter { public list} column public list> data } – Anupam Dutta Jul 27 '20 at 17:53

1 Answers1

0

This looks like it was originally serialized from some sort of nested data table structure. If you want to deserialize it into coherent strongly-typed classes, you will need a JsonConverter to do it. One possible solution would be to use the ArrayToObjectConverter<T> and JsonArrayIndexAttribute classes defined in How to deserialize a JSON array into an object using Json.Net? Then you could define your model classes as shown below. (You can rename the classes and properties to whatever makes sense for you. I just took guesses based on the JSON and some comments you made.)

public class RootObject
{
    [JsonProperty("data", ItemConverterType = typeof(ArrayToObjectConverter<Item>))]
    public List<Item> Items { get; set; }
}

public class Item
{
    [JsonArrayIndex(0)]
    public int EmployeeId { get; set; }
    [JsonArrayIndex(1)]
    public string EmployeeName { get; set; }
    [JsonArrayIndex(2)]
    public string CurrentAllocation { get; set; }
    [JsonArrayIndex(3)]
    public int DepartmentHead { get; set; }
    [JsonArrayIndex(4)]
    public string Manager { get; set; }
    [JsonArrayIndex(5)]
    public string Department { get; set; }
    [JsonArrayIndex(6)]
    public double Duration { get; set; }
    [JsonArrayIndex(7)]
    public EncounterData EncounterData { get; set; }
}

public class EncounterData
{
    [JsonProperty("data", ItemConverterType = typeof(ArrayToObjectConverter<Encounter>))]
    public List<Encounter> Encounters { get; set; }
}

public class Encounter
{
    [JsonArrayIndex(0)]
    public double StartTimestamp { get; set; }
    [JsonArrayIndex(1)]
    public double Duration { get; set; }
}

public class JsonArrayIndexAttribute : Attribute
{
    public int Index { get; private set; }
    public JsonArrayIndexAttribute(int index)
    {
        Index = index;
    }
}

And finally deserialize like this:

var root = JsonConvert.DeserializeObject<RootObject>(json);

Here is a working demo: https://dotnetfiddle.net/A7kp7L

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300