Your classes have properties that are of complex type ( classes
) so they need to be instantiated, so you have access to their most internal properties, look at the changes:
public class Name
{
public string type { get; set; }
}
public class Items
{
public string type { get; set; }
}
public class Hobbies
{
public Hobbies()
{
items = new Items();
}
public string type { get; set; }
public Items items { get; set; }
}
public class Properties
{
public Properties()
{
hobbies = new Hobbies();
name = new Name();
}
public Name name { get; set; }
public Hobbies hobbies { get; set; }
}
public class Example
{
public Example()
{
properties = new Properties();
}
public string description { get; set; }
public string type { get; set; }
public Properties properties { get; set; }
}
The sequence would be in the constructor of Hobbies
instanciar Items
, in the constructor of Properties
is instantiated Hobbies
and Name
and finally in of the constructor Example
is instantiated Properties
.
ONLine Project
If you do not want to do so, after instantiating Example
you can instantiate the classes as well, but I believe that the form I gave you is ideal, if you use all this code, an example with another form of work with these instances:
using Newtonsoft.Json;
public class Name
{
public string type { get; set; }
}
public class Items
{
public string type { get; set; }
}
public class Hobbies
{
public string type { get; set; }
public Items items { get; set; }
}
public class Properties
{
public Name name { get; set; }
public Hobbies hobbies { get; set; }
}
public class Example
{
public string description { get; set; }
public string type { get; set; }
public Properties properties { get; set; }
}
public class Program
{
public static void Main()
{
Example model = new Example();
model.properties = new Properties();
model.properties.name = new Name();
model.properties.hobbies = new Hobbies();
model.properties.hobbies.items = new Items();
model.properties.name.type = "cc";
var data = JsonConvert.SerializeObject(model);
System.Console.WriteLine(data);
}
}
OnLine Project