Tomimotmot
Tomimotmot

Reputation: 49

Serialize a nested Object (json.net)

I use Json.Net to serialize my objects, but now I need to serialize an nested object

sample serialize an object: https://www.newtonsoft.com/json/help/html/SerializeObject.htm

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

Account account = new Account
{
Email = "[email protected]",
Active = true,
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
Roles = new List<string>
{
    "User",
    "Admin"
}
};

string json = JsonConvert.SerializeObject(account, Formatting.Indented);
 // {
 //   "Email": "[email protected]",
 //   "Active": true,
 //   "CreatedDate": "2013-01-20T00:00:00Z",
 //   "Roles": [
 //     "User",
 //     "Admin"
 //   ]
 // }

Console.WriteLine(json);

But now I need a Json like:

 // {
 //   "Email": "[email protected]",
 //   "Active": true,
 //   "CreatedDate": "2013-01-20T00:00:00Z",
 //   "Roles": [
 //     "User"{
 //     "key": "value",
 //     "key": "value"
 //      }
 //     "Admin"
 //   ]
 // }

How can I build this Json?

Upvotes: 2

Views: 17729

Answers (2)

Satish Hirpara
Satish Hirpara

Reputation: 76

You need to create a separate/nested class as per your requirement. Please find below code and let me know if you require any additional information or change in it:

    private void Serialise()
    {

        //prepare static data
        List<User> users = new List<User>()
        {
            new User() {key = "value1"},
            new User() {key = "value2"}
        };

        Roles roles = new Roles();
        roles.Users = users;
        roles.Role = "Admin";

        Account account = new Account
        {
            Email = "[email protected]",
            Active = true,
            CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
            Roles = roles
        };

        //serialise
        string json = JsonConvert.SerializeObject(account, Formatting.Indented);
    }

    public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public Roles Roles { get; set; }
    }

    public class Roles
    {
        public List<User> Users { get; set; }
        public string Role { get; set; }
    }

    public class User
    {
        public string key { get; set; }
    }

Upvotes: 1

Kautilya Kumar
Kautilya Kumar

Reputation: 31

public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<Roles> Roles { get; set; }
    }

    public class Roles
    {
        public Dictionary<string, string> User { get; set; }

    }

static void Main(string[] args)
        {
    Account account = new Account
            {
                Email = "[email protected]",
                Active = true,
                CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
                Roles = new List<Roles> { new Roles { User=new Dictionary<string, string>
                                            {
                    {"Key","value" },
                    {"key","value" }
                                            }
                },

                },
            };

                string json=JsonConvert.SerializeObject(account,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
            Console.ReadLine();

}

Upvotes: 2

Related Questions