Aroma Aroma - 3 months ago 24
ASP.NET (C#) Question

Need to convert(Deserialize) following json into C# object

How to convert(deserialize) following json format in C# object?
All this json text convert(deserialize) in one go or I need to convert child object again and again


I am trying something like this

var x = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json_data);





{
"Id":"1405de4d-2823-43b4-8dba-66c2714bc7f",
"Name":"Sports/Boxing",
"Started":"\/Date(1472064057630)\/",
"DurationMilliseconds":227.2,
"MachineName":"RED",
"CustomLinks":null,
"Root":{
"Id":"88ada251-cff1-4eb7-bc47-2e6d366616a63",
"Name":"http://localhost:80/PDC/Sports/Boxing",
"DurationMilliseconds":227.2,
"StartMilliseconds":0,
"Children":[
{
"Id":"dbf36d18-8abd-43f1-ae9b-640cb3d77a87",
"Name":"Red Eagle",
"DurationMilliseconds":212,
"StartMilliseconds":15.1,
"Children":[
{
"Id":"dbd7018d-421d-42bd-b0e5-fd3e9462cca0",
"Name":"Blue Eagle",
"DurationMilliseconds":106.8,
"StartMilliseconds":120.4,
"Children":[
{
"Id":"c86199e0-d12b-4bd0-90ea-9f15a2618160",
"Name":"True Eagle",
"DurationMilliseconds":0.3,
"StartMilliseconds":226.9,
"Children":null,
"CustomTimings":null
}
],
"CustomTimings":null
}
],
"CustomTimings":null
}
],
"CustomTimings":null
},
"ClientTimings":null,
"User":"::1",
"Storage":null
}

Answer

Using http://json2csharp.com/ it produces this:

public class Child3
{
    public string Id { get; set; }
    public string Name { get; set; }
    public double DurationMilliseconds { get; set; }
    public double StartMilliseconds { get; set; }
    public object Children { get; set; }
    public object CustomTimings { get; set; }
}

public class Child2
{
    public string Id { get; set; }
    public string Name { get; set; }
    public double DurationMilliseconds { get; set; }
    public double StartMilliseconds { get; set; }
    public List<Child3> Children { get; set; }
    public object CustomTimings { get; set; }
}

public class Child
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int DurationMilliseconds { get; set; }
    public double StartMilliseconds { get; set; }
    public List<Child2> Children { get; set; }
    public object CustomTimings { get; set; }
}

public class Root
{
    public string Id { get; set; }
    public string Name { get; set; }
    public double DurationMilliseconds { get; set; }
    public int StartMilliseconds { get; set; }
    public List<Child> Children { get; set; }
    public object CustomTimings { get; set; }
}

public class RootObject
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime Started { get; set; }
    public double DurationMilliseconds { get; set; }
    public string MachineName { get; set; }
    public object CustomLinks { get; set; }
    public Root Root { get; set; }
    public object ClientTimings { get; set; }
    public string User { get; set; }
    public object Storage { get; set; }
}

But you can reduce it to this:

public class Child
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int DurationMilliseconds { get; set; }
    public double StartMilliseconds { get; set; }
    public List<Child> Children { get; set; }
    public object CustomTimings { get; set; }
}

public class Root
{
    public string Id { get; set; }
    public string Name { get; set; }
    public double DurationMilliseconds { get; set; }
    public int StartMilliseconds { get; set; }
    public List<Child> Children { get; set; }
    public object CustomTimings { get; set; }
}

public class RootObject
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime Started { get; set; }
    public double DurationMilliseconds { get; set; }
    public string MachineName { get; set; }
    public object CustomLinks { get; set; }
    public Root Root { get; set; }
    public object ClientTimings { get; set; }
    public string User { get; set; }
    public object Storage { get; set; }
}

Further tune the types of objects as necessary,

Then deserialize this way:

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