2

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?

0

2 Answers 2

2
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();

}
Sign up to request clarification or add additional context in comments.

Comments

1

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; }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.