Click or drag to resize
Json.NETSerialize an Object
 

This sample serializes an object to JSON.

Sample
Types
1public class Account
2{
3    public string Email { get; set; }
4    public bool Active { get; set; }
5    public DateTime CreatedDate { get; set; }
6    public IList<string> Roles { get; set; }
7}
Usage
 1Account account = new Account
 2{
 3    Email = "james@example.com",
 4    Active = true,
 5    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
 6    Roles = new List<string>
 7    {
 8        "User",
 9        "Admin"
10    }
11};
12
13string json = JsonConvert.SerializeObject(account, Formatting.Indented);
14// {
15//   "Email": "james@example.com",
16//   "Active": true,
17//   "CreatedDate": "2013-01-20T00:00:00Z",
18//   "Roles": [
19//     "User",
20//     "Admin"
21//   ]
22// }
23
24Console.WriteLine(json);