Justin Farrugia
Justin Farrugia

Reputation: 124

Convert JSON to C# complex object

I have this structure in C# and I want to create JSON that can be translated to it successfully so that I can easily find my mistake in the JS logic.

public class Tree
{
        public Root Root { get; set; }
        public Tree()
        {}
}

public class Root : Node
{
        public Root(int _id, int _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
            : base(_id, _fk, _name, _code, _children, _leaves)
        {
        }

        public Root()
        { }
}

public abstract class Node
{
        public int? ID { get; set; }
        public int? FK { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public List<Node> children { get; set; }
        public List<Item> leaves { get; set; }

        public Node(int? _id, int? _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
        {
            ID = _id;
            FK = _fk;
            Name = _name;
            Code = _code;
            children = _children;
            leaves = _leaves;
        }
        public Node ()
        {}
}

public class Item
{
        public string Code { get; set; }
        public string Desc { get; set; }
        public string Uom { get; set; }
        public float? Measurements { get; set; }
        public int? FK { get; set; }
        public int? Tier { get; set; }
        public bool IsADPresent { get; set; }
}

and the basic most JSON I am trying to feed in command JsonConvert.DeserializeObject<Tree>(tree) is:

{
    "Tree": {
        "Root": {
            "children": [],
            "leaves": [],
            "FK": 1,
            "ID": 1,
            "Name": " LOGISTICS",
            "Code": "PT01"
        }
    }
}

but I still get null in Tree; let alone when the JSON gets much more hairy (the tree can have up to the 5th grandchild).

Thanks

UPDATE: upon removing Tree from JSON I am given exception: Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type StatusUpdater.Models.NPoco.Node. Type is an interface or abstract class and cannot be instantiated. Path 'Root.children[0].children', line 1, position 33.'

Upvotes: 1

Views: 438

Answers (2)

An0d
An0d

Reputation: 211

You should remove Tree from your JSON as Tree is the destination type and don't need to be included in the JSON.

{
    "Root": {
        "children": [],
        "leaves": [],
        "FK": 1,
        "ID": 1,
        "Name": " LOGISTICS",
        "Code": "PT01"
    }
}

EDIT : In order to deserialize your abstract Node elements, you will need a concrete type and a custom converter. See Deserializing a collection of abstract classes and all duplicate links listed

Upvotes: 3

tmaj
tmaj

Reputation: 35017

For this json you will need to create a wrapper class.

public class X { public Tree {get; set;} } 

(...)

var tree = JsonConvert.DeserializeObject<X>(tree)?.Tree;


Why?

Let's examine the json.

{
    "Tree": {
        "Root": {
            "children": [],
            "leaves": [],
            "FK": 1,
            "ID": 1,
            "Name": " LOGISTICS",
            "Code": "PT01"
        }
    }
}

This json contains

        1 object with a property "Tree"

                which in turn has one property "Root"

                         which contains multiple properties

Upvotes: 1

Related Questions