Click or drag to resize
Json.NETPopulate an Object
 

This sample populates an existing object instance with values from 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 List<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 = @"{
14  'Active': false,
15  'Roles': [
16    'Expired'
17  ]
18}";
19
20JsonConvert.PopulateObject(json, account);
21
22Console.WriteLine(account.Email);
23// james@example.com
24
25Console.WriteLine(account.Active);
26// false
27
28Console.WriteLine(string.Join(", ", account.Roles.ToArray()));
29// User, Admin, Expired