CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string json = @"
{
""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
]
]
}
]
]
}";
var root = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var item in root.Items)
{
Console.WriteLine("EmployeeId: " + item.EmployeeId);
Console.WriteLine("EmployeeName: " + item.EmployeeName);
Console.WriteLine("CurrentAllocation: " + item.CurrentAllocation);
Console.WriteLine("DepartmentHead: " + item.DepartmentHead);
Console.WriteLine("Manager: " + item.Manager);
Console.WriteLine("Department: " + item.Department);
Console.WriteLine("Duration: " + item.Duration);
Console.WriteLine("Encounters: ");
foreach (var encounter in item.EncounterData.Encounters)
{
Console.WriteLine(" StartTimestamp: " + encounter.StartTimestamp);
Console.WriteLine(" Duration: " + encounter.Duration);
Console.WriteLine();
}
}
}
}
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;
}
}
public class ArrayToObjectConverter<T> : JsonConverter where T : class, new()
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray array = JArray.Load(reader);
var propsByIndex = typeof(T).GetProperties()
.Where(p => p.CanRead && p.CanWrite && p.GetCustomAttribute<JsonArrayIndexAttribute>() != null)
.ToDictionary(p => p.GetCustomAttribute<JsonArrayIndexAttribute>().Index);
JObject obj = new JObject(array
.Select((jt, i) =>
{
PropertyInfo prop;
return propsByIndex.TryGetValue(i, out prop) ? new JProperty(prop.Name, jt) : null;
})
.Where(jp => jp != null)
);
T target = new T();
serializer.Populate(obj.CreateReader(), target);
return target;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}