What is the correct class structure in C# to convert this Json into?

+1 vote
asked Sep 11, 2015 by leora

I have the following Json below coming from a Rest service and I am trying to deserialize it into a C# object using this code:

 var _deserializer = new JsonDeserializer();
 var results = _deserializer.Deserialize<Report>(restResponse);

The deserialize method keeps returning null which tells me that my C# object is not structured correctly.

Below is the Json and my latest attempt at the C# definition.

{
    "Report": [
        {
            "ID": "0000014",
            "Age": "45",
            "Details": [
                {
                    "Status": "Approved",
                    "Name": "Joe"
                },
                {
                    "Status": "Approved",
                    "Name": "Bill"
                },
                {
                    "Status": "Submitted",
                    "Name": "Scott"
                }
            ]
        },
        {
            "ID": "10190476",
            "Age": "40",
            "Details": [
                {
                    "Status": "Approved",
                    "Name": "Scott"
                }
            ]
        },
        {
            "ID": "10217480",
            "Age": "40",
            "Details": [
                {
                    "Status": "Approved",
                    "Name": "Scott"
                }
            ]
        }
    ]
}

Here is my C# object:

public class Report
{
    public List<WorkItem> Item= new List<WorkItem>(); 
}

public class WorkItem
{
    public string ID { get; set; }
    public int Age { get; set; }

    public List<Details> Details { get; set; }
}

public class Details
{
    public string Status { get; set; }
    public string Name { get; set; }
}

Can someone advise what is wrong with my C# object definition to make this json deserialize correctly?

3 Answers

+2 votes
answered Sep 11, 2015 by tam

Try changing the Report class like so (The class name can be anything, the property must be Report)

public class WorkReport
{
    public List<WorkItem> Report;
}

It should be trying to deserialize at the root into a class with an array/list of of workitem objects called Report.

0 votes
answered Sep 11, 2015 by mohit-shrivastava

You can try something like this. I have changed List to Dictionary You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle. Whenever you have an object whose keys can change, you need to use a Dictionary. A regular class won't work for that; neither will a List.

public class RootObject
{
    [JsonProperty("Report")]
    public Report Reports { get; set; }
}
public class Report
{
    [JsonProperty("Report")]
    public Dictionary<WorkItem> Item;
}

public class WorkItem
{
    [JsonProperty("ID")]
    public string ID { get; set; }
    [JsonProperty("Age")]
    public int Age { get; set; }
    [JsonProperty("Details")]
    public Dictionary<Details> Details { get; set; }
}

public class Details
{
    [JsonProperty("Status")]
    public string Status { get; set; }
    [JsonProperty("Name")]
    public string Name { get; set; }
}

Then, deserialize like this:

Report results = _deserializer.Deserialize<Report>(restResponse);
+3 votes
answered Sep 11, 2015 by ertdiddy

I would recommend using Json2Csharp.com to generate the classes.

public class Detail
{
    public string Status { get; set; }
    public string Name { get; set; }
}

public class Report
{
    public string ID { get; set; }
    public string Age { get; set; }
    public List<Detail> Details { get; set; }
}

public class RootObject
{
    public List<Report> Report { get; set; }
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...