newtonsoft serialize object code example
Example 1: c# serialize json
using System.Text.Json;
var jsonString = JsonSerializer.Serialize(yourObject);
var obj = JsonSerializer.Deserialize<YourObject>(stringValue);
Example 2: how to convert object in string JSON c#
var json = new JavaScriptSerializer().Serialize(obj);
Example 3: c# newtonsoft json serialize
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);
Console.WriteLine(json);
Example 4: c# json convert
jsonString = File.ReadAllText(fileName);
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);
Example 5: c# newtonsoft json serialize
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}